Add sign-in with BillDog Auth
To add sign-in with BillDog Auth, enable it under Project Settings → Settings → Auth and provision signing keys. Then install @billdog.io/auth, create a client with your public key and call signUp and attempt from your own form. The session token carries the user's entitlements, and your backend checks it offline with verifyToken from billdogeng-node.
Steps last checked against the SDK source on 15 Nov 2026.
What you'll build
- Email and password sign-up and sign-in on your own web page
- A session the BillDog web SDK uses, so purchases and entitlements follow the signed-in user
- A Node route that verifies the session token and gates on an entitlement without calling BillDog
What you'll need
- A BillDog project in the US data region (sign up)
- An email provider you can connect (verification codes are sent through it)
- A web app built with npm, and a Node backend for the last step
1. Turn on Auth for the project
6 minAuth is off by default, and every sign-up and sign-in endpoint answers 403 ACCESS_DENIED until you turn it on. Two things must be in place before the switch unlocks, so do them in this order:
- Open Engagement → Messaging, then Settings → Email, and connect an email provider. Sign-up codes are sent from your provider, and the switch stays disabled without one.
- Check the data region on Project Settings → Settings → Privacy. Auth credentials may only be stored in the US, so enabling is refused on any other region.
- Open Project Settings → Settings → Auth and turn on Enable authentication.
- In the Signing keys card on the same tab, check for a Provision button. Turning Auth on creates a signing key for each environment; if the button shows, an environment has no key, so click it, or sign-in there fails with
500 No signing key is provisioned.
Hosted or SDK? Enabling Auth also gives you a hosted address, <name>.billdog.app, that can serve a sign-in wall you design in the builder. That path needs no sign-in code. This codelab uses the SDK instead, so the form lives on your own page. See Hosted or SDK? to compare.
2. Install the SDK and create the client
3 minPin the exact version. The package is a pre-release, and a dist-tag like @beta can point at an older build.
npm install @billdog.io/auth@1.2.0-beta.1Copy a public key from Integration & Data → API Keys. A bd_public_test_ key reaches your sandbox, whose users are separate from production's. Public keys are safe in the browser.
import { createBillDogAuth } from '@billdog.io/auth';
export const auth = createBillDogAuth({
apiKey: 'bd_public_test_YOUR_KEY',
onSessionChange: (session) => {
// Fires on sign-in, refresh, sign-out and expiry.
renderHeader(session ? 'signed-in' : 'signed-out');
},
});
// renderHeader() is your own UI function.3. Sign a user up
4 minsignUp never signs anyone in. It emails a code, and attempt with strategy: 'email_code' finishes the sign-up and returns the session.
import { auth } from './auth';
export async function startSignUp(email: string, password: string) {
await auth.signUp({ identifier: email, password });
// Resolves the same way whether or not the email was already registered.
}
export async function finishSignUp(email: string, code: string) {
const result = await auth.attempt({
identifier: email,
strategy: 'email_code',
code, // the 6-digit code from the email
});
return result.session;
}The reply never tells you whether the address already had an account. That is deliberate: a different reply would let anyone test which emails are registered. If bot protection is on and you send no captcha token, signUp answers 400; a failed captcha, or a missing invitation on an invite-only project, answers 403 ACCESS_DENIED. Call auth.config() to see what the project requires.
4. Sign in and read the session
4 minPassword sign-in is also attempt. Check mfaRequired first: for an account with two-factor authentication, a correct password returns no session, only an mfaToken.
import { auth } from './auth';
export async function signIn(email: string, password: string) {
const result = await auth.attempt({
identifier: email,
strategy: 'password',
password,
});
if (result.mfaRequired) {
// Ask for the authenticator code, then:
// await auth.attempt({ strategy: 'totp', code, mfaToken: result.mfaToken });
return null;
}
return result.session;
}
// Anywhere in your app:
auth.isSignedIn; // true while the token is unexpired
auth.session?.customerId; // the BillDog customer this user owns purchases as
auth.session?.sessionId;Wrong password, unknown email and expired code all return the same 401, on purpose. Email and password are one path. The same SDK also has email codes (requestCode plus attempt), SMS codes, Google, Apple and Facebook (signInWithRedirect) and passkeys. auth.config() lists the social providers you have configured under Project Settings → Settings → OAuth.
5. Sign out, and know what a reload does
3 minimport { auth } from './auth';
export async function signOut() {
await auth.signOut(); // this device
// await auth.signOut('all'); // every device
}
// signOut never throws. It always clears the local session and reports
// whether the server-side revoke landed: { revokedRemotely: boolean }The session token lives in memory only and lasts 90 seconds. The SDK refreshes it for you before it expires, so a single-page app stays signed in.
A full page reload clears memory. auth.restore() can bring the session back only from the httpOnly refresh cookie, which exists on your project's hosted auth domain. When your page calls the API from another origin, restore() returns null after a reload and the user signs in again. If sessions must survive reloads, use the hosted sign-in page.
6. Link the session to the user's purchases
3 minA user is who signs in. A customer is what they own: purchases and entitlements. The session carries the customer as customerId. If you also use the BillDog web SDK, pass the auth client to configure. The SDK then sends the session on every call, so entitlement checks answer for the signed-in customer.
import BillDog from '@billdog.io/web';
import { auth } from './auth';
BillDog.configure({
apiKey: 'bd_public_test_YOUR_KEY',
auth, // the same @billdog.io/auth instance
});The session token also carries the entitlements themselves, in the ent claim, plus the customer id in cus. That is what lets your backend gate a feature without a round trip, which is the next step.
7. Verify the token on your backend
5 minSend the token to your own API with auth.authHeaders(). It sets the X-BillDog-Session header, not Authorization: Bearer.
const res = await fetch('/api/pro-report', { headers: auth.authHeaders() });On the server, install billdogeng-node at the same exact version. verifyToken checks the Ed25519 signature against the project's key set (JWKS). The key set is fetched once and cached (10 minutes by default), so most requests make no network call.
npm install billdogeng-node@1.2.0-beta.1import express from 'express';
import { verifyToken, hasEntitlement, entitlementsComplete } from 'billdogeng-node';
const app = express();
app.get('/api/pro-report', async (req, res) => {
const token = req.header('X-BillDog-Session');
if (!token) return res.status(401).end();
try {
const claims = await verifyToken(token, {
projectId: process.env.BILLDOG_PROJECT_ID!,
environment: 'sandbox', // tokens from bd_public_test_ keys; omit for production
});
if (!hasEntitlement(claims, 'premium')) {
// false can also mean the entitlement list was cut off at 64
if (!entitlementsComplete(claims)) console.warn('ent claim truncated');
return res.status(403).end();
}
res.json({ user: claims.sub, customer: claims.cus });
} catch {
res.status(401).end(); // expired, forged, wrong project or wrong environment
}
});
app.listen(3000);environment defaults to production, and a production verifier rejects sandbox tokens. Your project ID is the UUID in the dashboard URL of Project Settings → Team (/dashboard/projects/<id>/team); the token's aud claim must match it. Before release, switch to the bd_public_live_ key and drop the environment option.