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:
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).
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
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:
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:
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 theexport. If you leave it outside, importing the file without the variables (from a test, for example) fails withSet appUrl or allowedOrigins. If your tests create the app with their own configuration, give them test values too, likeappUrl: 'http://localhost', or they fail with that same error. - Your app reads
SESSION_SECRETfrom a configuration file (aconfig.js, anenv.ts, a zod schema…): addBLOCKAUTH_API_KEY,BLOCKAUTH_API_SECRETandAPP_URLthere too, and in the block change eachprocess.env.…to your configuration value (for example,config.blockauthApiKey). The block still goes belowconst 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
app.use(
'/api',
express.json(),
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: { httpOnly: true, sameSite: 'lax' },
}),
)
After
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:
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 thedotenvpackage (import 'dotenv/config'at the top of your entry file). - Behind a proxy that terminates
https, addapp.set('trust proxy', 1).
This only checks that everything is in place. The real test comes in the last step, Connect and try it.

