Launch offer for the first 500 accounts: $10K MTR free for 24 months + $100 credit →
    .io
    Auth
    Web
    Intermediate
    28 min

    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 min

    Auth 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:

    1. 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.
    2. 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.
    3. Open Project Settings → Settings → Auth and turn on Enable authentication.
    4. 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 min

    Pin the exact version. The package is a pre-release, and a dist-tag like @beta can point at an older build.

    Terminal
    npm install @billdog.io/auth@1.2.0-beta.1

    Copy 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.

    auth.ts
    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.
    Next: Sign a user up

    3. Sign a user up

    4 min

    signUp never signs anyone in. It emails a code, and attempt with strategy: 'email_code' finishes the sign-up and returns the session.

    signup.ts
    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 min

    Password sign-in is also attempt. Check mfaRequired first: for an account with two-factor authentication, a correct password returns no session, only an mfaToken.

    signin.ts
    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 min
    signout.ts
    import { 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.

    7. Verify the token on your backend

    5 min

    Send the token to your own API with auth.authHeaders(). It sets the X-BillDog-Session header, not Authorization: Bearer.

    client.ts
    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.

    Terminal
    npm install billdogeng-node@1.2.0-beta.1
    server.ts (Express)
    import 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.

    Frequently asked questions

    The switch unlocks only when the project has an active email provider and stores data in the US region. Connect a provider under Engagement, Messaging, Settings, Email, and check the region on the Privacy tab of Project Settings.

    Enabling Auth creates signing keys, but a project enabled earlier can lack a key for one environment. Click Provision in the Signing keys card on the Auth tab. It creates a key only for environments that have none and leaves existing keys alone.

    Yes, from Python and Go. Three server SDKs verify offline against the cached key set: billdogeng-node (verifyToken), the Python package billdogeng on PyPI (verify_token) and the Go module github.com/billdogai/billdogeng-go (auth.VerifyToken). From other languages, call the introspection endpoint, or verify the Ed25519 signature against the JWKS with a generic JWT library and check the audience, environment and entitlement truncation yourself.

    90 seconds. The SDK refreshes it before it expires, and each refresh rotates the refresh token. A revoked session can still pass offline verification until its current token expires.

    Reference

    Ship it on a free project

    Create a project, grab your SDK key and follow along. Sandbox purchases and data stay separate from production, so test transactions never mix with real revenue.

    We use cookies and process your data to provide our services. By continuing, you agree to our Terms and Privacy Policy.