Skip to main content
signToken creates a compact, short-lived token that your frontend can use with the "GatelitSigned" auth scheme. Call it from your backend — never from client-side code.
The function uses the Web Crypto API, available natively in Node.js 18+ and all edge runtimes (Cloudflare Workers, Vercel Edge, Deno). The returned string is a compact dot-separated value: base64url(header).base64url(payload).base64url(signature) — structurally identical to a standard HS256 JWT.
Never call signToken or expose GATELIT_SIGNING_SECRET in frontend code. Import @gatelit/sdk/sign only from server-side files (API routes, server functions, edge middleware).

Parameters

options

string
required
User identifier embedded in the token. Used for request attribution in dashboard logs.
string
Organisation ID to embed in the token. The gateway uses this to resolve org-scoped provider keys when BYOK is configured.
string
Restrict this token to a single model in "provider/model-name" format (for example "openai/gpt-4o"). Requests using a different model are rejected by the gateway. Omit to allow any model permitted by your policy.
number
Cap the maximum output tokens for requests made with this token.
number
default:"60"
Token lifetime in seconds. Defaults to 60. Tokens expire at iat + ttl.

secret

string
required
Your GATELIT_SIGNING_SECRET. Store this in an environment variable and never expose it outside your server.

Examples

Nuxt server route

Next.js API route

Using the token on the frontend

Once your backend endpoint issues a token, pass it to GatelitClient via getToken:
getToken is called before every request. If your users make several requests in quick succession, consider caching the token in a variable and only re-fetching a new one when the current token is within a few seconds of expiry.

Security notes

  • Never import @gatelit/sdk/sign in browser code. The separate import path exists specifically to prevent the signing code from being bundled into client builds.
  • Store the secret in an environment variable. Use process.env.GATELIT_SIGNING_SECRET (or your runtime’s equivalent) and ensure it is excluded from version control.
  • Authenticate the user before issuing a token. signToken does not perform any authentication — you must verify the caller’s identity in your own middleware before calling it.
  • Keep TTLs short. The default of 60 seconds is appropriate for most use cases. A shorter TTL limits the window of exposure if a token is intercepted.