SDK core.
Check each request with the server in one call, act for a person, and verify webhooks, from Node, Bun, or Deno.
The backend SDK of the tensile-auth server. A Node, Bun, or Deno app uses it to check each request with the server in one call, to act for a person, and to verify webhooks. It has no runtime dependencies: it uses only fetch, Web Crypto, and Web Streams.
It is versioned with the Go library:
npm install @tensile-tools/auth @tensile-tools/auth-clientUse it without a framework
import { createTensile } from '@tensile-tools/auth'
const auth = createTensile({ url: process.env.TENSILE_AUTH_URL!, appKey: process.env.TENSILE_AUTH_APP_KEY! })
const r = await auth.check({
credential: { cookie: sessionCookieValue },
request: { method: 'DELETE', url: 'https://app.example.com/repos/7', client_ip: ip },
need: { action: 'repo.delete', human: true, assurance: 'multi', within: '10m' },
})
if (!r.allowed) return problemResponse(r.problem)check({credential, request, need?, approval?, resource?, signal?})callsPOST /v1/check. It returns{allowed: true, principal}or{allowed: false, problem}, withset_cookiesandheadersto copy onto the app’s response. It never throws for a refusal. It throws aTensileErroronly when the server can’t decide: an outage, a timeout, or a bad or throttled app key. Answer those with 503, never 401.credentialOf(request)reads a request’s credential asauthhttp.Middlewaredoes:Authorizationfirst, withDPoPfor the DPoP scheme, then the__Host-sessioncookie.requestInfo(request, clientIp)describes a request forcheck.as(subject).agents.create,.agents.delegate,.approvals.request, and.sessions.treeact for a person.subjectis that person’s credential, or a request that carries it: the app key alone never acts for anyone. A refusal throws aProblemError.users.get(id)returns a user’s summary,{id, email, email_verified, live_sessions, agents, last_seen_at}, orundefined.meta()returns the server’s version. The first call to the server also checks it, and warns when the server is older than the SDK.webhooks.verify(request)checks a Standard Webhooks signature withwebhookSecret, refuses a timestamp more than 5 minutes from now, and returns the event. Hooks such assign_upare signed the same way.webhooks.answer(event, answer)answers a hook,sign_upwith{allow, user_id?}orsession_scopeswith{scopes}, signed for the hook’s ownwebhook-id. The server refuses an answer without that signature and fails closed: a sign-up gets 503, and a session gets no scopes.proxy(routes)returns the proxy for the browser routes, a Fetch API handler. It forwards/auth/, and/admin/,/login/oauth/,/login/device/, and/.well-known/only whenroutesturns them on. It forwards only the library’s cookies and a fixed set of headers, adds the app key and the client’s address, never returns the app key, and caps request bodies at 1 MiB.onFirstSeen(principal)runs the first time the process sees a user ID, before the request continues, so the app can create its own row for that user.
createTensile refuses an http:// server URL unless the host is loopback, a private address, or a single-label name such as tensile-auth on a Compose network. Every call has a 5-second timeout, set with timeoutMs, and check never retries; a check that waits for an approval gets its wait on top. The proxy waits up to 30 seconds for the /auth/ routes, and up to 65 seconds for /auth/claims/, which long-polls for up to 60 seconds.
Wait for an approval
An agent that an approvable route refuses gets an approval and retries with Tensile-Approval: <id>. With Prefer: wait=N on the retry, the route holds the request until the person approves or denies, and runs once, so the agent doesn’t poll. The server holds the app’s check call, so the app keeps no state between the two requests.
approvalWait, in seconds from 0 to 60 or a duration such as '25s', caps the wait and is the wait the refusal offers in wait_hint:
| Where the app runs | Default | Why |
|---|---|---|
| A long-running server | 50 | Under the common 60-second idle timeout of load balancers |
A detected serverless platform: VERCEL, AWS_LAMBDA_FUNCTION_NAME, NETLIFY, or an edge runtime |
0 | The SDK can’t read the function’s limit, and a function stopped mid-wait can spend the approval without running the route. The agent retries after Retry-After instead |
On serverless, turn waiting on with the function’s limit, and keep maxDuration >= approvalWait + 5:
// app/api/repos/[id]/route.ts, on Vercel
export const maxDuration = 30
export const DELETE = auth.approvable(need, describe, handler, { wait: 25 })- The wait is the smallest of the agent’s
Prefer, the route’swaitorapprovalWait, 60 seconds, and the approval’s remaining life. A retry withoutPreferdoesn’t wait. approvablein each adapter takes{ wait }to replaceapprovalWaitfor one route.auth.approvalWaitreports the value in effect.- A waiting check’s timeout is its wait plus
timeoutMs, so a wait that runs out isapproval_pendingwithRetry-After, never aTensileError. - The check ends when the agent disconnects. Express builds the signal from the response’s
closeevent; Hono and Next.js usereq.signal. Without an adapter, passsignalandapproval.waittocheck. - The server holds at most 4 waits for each agent and 256 for each app key on each replica; over either, the retry answers at once. A server older than this ignores
Preferand answers at once.
Adapters
| Import | For |
|---|---|
@tensile-tools/auth/next |
Next.js 16 and later, App Router |
@tensile-tools/auth/express |
Express 5 |
@tensile-tools/auth/hono |
Hono 4 |
Each adapter has the same parts: a proxy for the browser routes, a principal, a need, an approvable need, and calls on a person’s behalf. Its framework is an optional peer dependency.
The contract
src/gen/api.ts is generated from the server’s server/openapi.json. To regenerate it after the document changes, run npm run gen in sdk/. just sdk fails when the committed file differs from a fresh generation.
src/api.ts narrows the generated types, for example assurance to its 4 values, and test/contract.ts fails the type-check when the SDK sends or reads a field the document lacks.
just sdk-e2e builds the server, starts it with SQLite, and checks the SDK against it with a real app key and a real passkey sign-up: /v1/check, the routes that act for a person, an agent’s approval from request to redeem, approval waits, the proxy, the Hono adapter, the cache, and hook answers.