Your backend

Add the routes the widget calls and receive webhooks. With the Block-Auth package for your server it's mostly configuration: it redeems the login code, keeps the session and verifies the webhooks for you. If you prefer, you can write it yourself.

Your environment variables

Your backend reads these variables. Here they are with your data:

These files use example values. Fill in your data once in the SDK Quickstart and they come out with yours, ready to copy.Fill in my data

In a .env next to your backend package.json. Load it with node --env-file=.env (Node 20.6+) or the dotenv package (import 'dotenv/config' at the top of your entry file).

Your backend’s .env · in your backend folder
APP_URL=http://localhost:5173
BLOCKAUTH_API_KEY=your_api_key
BLOCKAUTH_API_SECRET=<paste the API Secret from the Dashboard>
SESSION_SECRET=<generate one: openssl rand -hex 32>

Replace the API Secret placeholder with your API Secret, copied straight from the Dashboard. Never paste it on this site.

Replace the SESSION_SECRET placeholder with a random value generated on your computer: openssl rand -hex 32.

Replace your_api_key with your API Key, or fill in your data in the SDK Quickstart and it is filled in for you.

How you add the backend login routes

Applies from here on. You pick it once in the SDK Quickstart.

Add the backend routes

1. Install the package

Run it next to your backend’s package.json (if your frontend and backend share one project, at the root):

Node.js

Terminal · next to your backend package.json
npm install @block-auth.io/node

The package works with Express and express-session. Check your package.json dependencies: if you already have both, skip this. If one is missing, install it: npm install express express-session.

2. Add it to your server

This is everything you add to your server, in three parts. Your app, your session and your server start-up stay as they are.

a. The imports, at the top of the file where you create your app, next to the ones you already have:

Top of the file · with your imports
import { BlockAuthRoutes } from '@block-auth.io/node'
import { blockauthRouter } from '@block-auth.io/node/express'

b. Create routes. Don’t paste your keys here: the block reads them from the .env you filled in under “Your environment variables”, so the API Secret never ends up in your code. Where it goes, right below the block:

Below const app = express()
export const routes = new BlockAuthRoutes({
  apiKey: process.env.BLOCKAUTH_API_KEY,
  apiSecret: process.env.BLOCKAUTH_API_SECRET,
  appUrl: process.env.APP_URL,
})

Where to put it: in the file with const app = express(), right below that line. Two adjustments, only if your code looks like this:

  • That line is inside a function, like function createApp(config) {: put the block inside the function, also below it, and drop the export. If you leave it outside, importing the file without the variables (from a test, for example) fails with Set appUrl or allowedOrigins. If your tests create the app with their own configuration, give them test values too, like appUrl: 'http://localhost', or they fail with that same error.
  • Your app reads SESSION_SECRET from a configuration file (a config.js, an env.ts, a zod schema…): add BLOCKAUTH_API_KEY, BLOCKAUTH_API_SECRET and APP_URL there too, and in the block change each process.env.… to your configuration value (for example, config.blockauthApiKey). The block still goes below const app = express().

If you see Set appUrl or allowedOrigins on startup, the variables aren’t reaching your server: check that it loads the .env (for example, with node --env-file=.env, Node 20.6+, or with import 'dotenv/config' at the top of your entry file).

Before mounting it, search for session( in your file. If it’s on its own, in app.use(session(...)), change nothing: in step c you put the line right below it. If it’s inside an app.use('/api', …) together with other things, like express.json(), it only covers /api and Block-Auth can’t see it. Split it like this: the session leaves the app.use('/api', …) with its options as they are, Block-Auth goes right below it and the rest stays under /api:

Before

Everything together under /api
app.use(
  '/api',
  express.json(),
  session({
    secret: process.env.SESSION_SECRET,
    resave: false,
    saveUninitialized: false,
    cookie: { httpOnly: true, sameSite: 'lax' },
  }),
)

After

The session leaves /api · Block-Auth belowNew from Block-Auth
app.use(
  session({
    secret: process.env.SESSION_SECRET,
    resave: false,
    saveUninitialized: false,
    cookie: { httpOnly: true, sameSite: 'lax' },
  }),
)
Copyapp.use(blockauthRouter(routes))
app.use('/api', express.json())

c. Mount it in your app. This is the part where placement matters most. Put it where routes already exists: after step b and in the same place. If you created routes inside a function, like createApp() or create_app(), this line goes inside that same function. In Python that means with the same indentation as the routes = ... line; if you paste it against the left margin, it ends up outside the function and Python raises an error:

Right below where you mount session(...)
app.use(blockauthRouter(routes))

Put it before serving your frontend (express.static(...)) and before any catch-all route (app.get('*', …)). If those come first, they take the widget’s requests and Block-Auth never sees them: you get no error, just a 404 or your app’s HTML.

3. Checklist

Go over these before moving on. They’re what most often fails when adding it to an existing app:

Node.js

  • The session covers the whole app: app.use(session(...)), not just /api.
  • The order is session → blockauthRouter → express.json().
  • You load the .env: node --env-file=.env (Node 20.6+) or the dotenv package (import 'dotenv/config' at the top of your entry file).
  • Behind a proxy that terminates https, add app.set('trust proxy', 1).

This only checks that everything is in place. The real test comes in the last step, Connect and try it.