Skip to content
On this page

Gate an Express route

Ten lines, no SDK gymnastics. The @orangecheck/gate package ships a middleware factory that does the whole verify-and-threshold cycle for you.

InstallLink to this section

npm install @orangecheck/gate

Minimal exampleLink to this section

TypeScript
import { ocGate } from '@orangecheck/gate';
import express from 'express';

const app = express();

app.post(
    '/post',
    ocGate({
        minSats: 100_000,
        minDays: 30,
        address: { from: (req) => req.get('x-bitcoin-address') ?? '' },
    }),
    (_req, res) => res.json({ ok: true, posted: true })
);

app.listen(3000);

What happens on a request:

  1. The middleware extracts the address from wherever you pointed it (a header, a cookie, a session, a query param — your call).
  2. It runs the SDK's check() in-process: it finds the attestation on Nostr relays, verifies its BIP-322 signature and reads the bonded coins from Esplora. No orangecheck server sits in the path.
  3. On pass: request proceeds to your handler.
  4. On fail: returns 403 with a JSON body whose reason says why (below_threshold, not_found, …).

Address sourcesLink to this section

Pick one based on your trust model:

TypeScript
import { ocGate } from '@orangecheck/gate';

// From a header (cheap, trust the client)
ocGate({ address: { from: 'header', name: 'x-bitcoin-address' } });

// From a signed session (trustworthy, set by your sign-in route)
ocGate({ address: { from: (req) => req.session?.verifiedAddress } });

// From the query string
ocGate({ address: { from: 'query', name: 'addr' } });

// From the JSON body (a dot-path)
ocGate({ address: { from: 'body', path: 'addr' } });

Warning. Reading the address from a header / cookie / query without a signed-challenge step means you're trusting the caller to tell you who they are. For low-stakes gates (public forum, throttling) this is fine. For anything where the cost of impersonation is real, pair this guide with Sign in with Bitcoin so the address comes from a session cookie you issued.

Custom rejection responseLink to this section

TypeScript
import { ocGate } from '@orangecheck/gate';
import express from 'express';

const app = express();

app.post(
    '/post',
    ocGate({
        minSats: 100_000,
        minDays: 30,
        address: { from: (req) => req.get('x-bitcoin-address') ?? '' },
        onBlocked: (req, res, decision) => {
            res.status(403).json({
                error: 'not enough stake',
                reason: decision.reason,
                requires: { sats: 100_000, days: 30 },
                you_have: {
                    sats: decision.check?.sats ?? 0,
                    days: decision.check?.days ?? 0,
                },
            });
        },
    }),
    (_req, res) => res.json({ ok: true, posted: true })
);

decision.reason says why the gate blocked, and decision.check is the SDK's check() result whenever a lookup happened, so you can tailor the error body to your use case (redirect to a "how to bond" page, send back a call-to-action, etc.).

CachingLink to this section

ocGate keeps each lookup in an in-process cache for 60 seconds by default, so rapid retries don't repeat the relay and Esplora reads. Tune it with cacheTtlMs (capped at 10 minutes) and cacheMax. An option the gate does not read throws when the middleware is built.

Your own relaysLink to this section

The gate reads attestations from public Nostr relays. Point it at relays you run:

TypeScript
import { ocGate } from '@orangecheck/gate';

ocGate({
    minSats: 100_000,
    address: { from: 'header', name: 'x-bitcoin-address' },
    relays: ['wss://relay.internal.example'],
});

Or call the SDK's check() yourself, when your handler needs the result rather than a 403:

TypeScript
import { check } from '@orangecheck/sdk';
import express from 'express';

const app = express();

app.post('/post', async (req, res, next) => {
    const addr = req.get('x-bitcoin-address') ?? '';
    const result = await check({
        addr,
        minSats: 100_000,
        minDays: 30,
    });
    if (!result.ok) return res.status(403).json({ reasons: result.reasons });
    next();
});

The SDK does all the work in-process — Nostr lookup, BIP-322 verification, Esplora fetch, threshold check. No round-trip to any orangecheck-operated server.

See alsoLink to this section