Edge Functions
Write a function, deploy it, call it as a signed-in user, keep its secrets out of the code and roll back a bad deploy.
An Edge Function is TypeScript that answers HTTP requests at
https://<project>.nulvless.co/functions/<slug>. Use one for an API your app
calls, a webhook from Stripe or GitHub, a call to an AI model, or any code that
needs a secret the browser must never see.
Write one
Every function receives a web standard Request and returns a Response:
export default function handler(req: Request): Response {
const name = new URL(req.url).searchParams.get('name') ?? 'world';
return Response.json({ message: `Hello, ${name}!` });
}curl "https://<project>.nulvless.co/functions/hello?name=Ada"The default export is one of three shapes, and they all behave the same:
export default function handler(req: Request): Response {
return Response.json({ ok: true });
}Node apps that call listen(), such as Express, run as they are.
What every function gets
- Secrets through
Deno.env.get('NAME').NULVLESS_URL,NULVLESS_FUNCTION_SLUGandNULVLESS_FUNCTION_VERSIONare always set. - npm and JSR packages, imported directly (
npm:stripe) or through an import map in adeno.jsonnext toindex.ts. - The rest of the path. A request to
/functions/hello/a/breaches the code as/<project>_hello/a/b: the first segment is the function's own, and what follows it is yours to route.
Deploy
Keep functions in your repository with a workspace, one folder per function, and deploy them from the terminal or CI:
nulvless functions new checkout # nulvless/functions/checkout/index.ts
nulvless functions deploy checkoutA function written in the console's editor comes into the workspace with
nulvless functions pull checkout. To start from a working example, such as a
Stripe webhook or a streamed AI chat, see
github.com/nulvless/examples:
nulvless functions new whoami --template verify-sessionCall it as a signed-in user
The function receives the request exactly as it was sent. Nothing in front of it checks the access token, so the function verifies it with the SDK before trusting who the caller is:
import { createServerClient } from '@nulvless/nulvless-js';
const nulvless = createServerClient(Deno.env.get('NULVLESS_URL')!, Deno.env.get('PROJECT_SECRET_KEY')!);
export default async function handler(req: Request): Promise<Response> {
const authorization = req.headers.get('authorization');
if (!authorization) return Response.json({ error: 'Sign in to call this function.' }, { status: 401 });
const { data, error } = await nulvless.auth.verify(undefined, { headers: { Authorization: authorization } });
if (error) return Response.json({ error: 'Your session is not valid. Sign in again.' }, { status: 401 });
return Response.json({ user: data.user.id, assurance_level: data.session.assurance_level });
}Your app sends the user's access token:
const token = await nulvless.auth.getAccessToken();
await fetch('https://<project>.nulvless.co/functions/whoami', {
headers: { Authorization: `Bearer ${token}` },
});To allow only some roles, pass them to verify with the resource they apply to:
const { error } = await nulvless.auth.verify(
{ resource: organization, roles: ['owner', 'admin'] },
{ headers: { Authorization: authorization } },
);Decoding the token yourself is not a check: anyone can write a token with any
user in it. Only verify proves the session is real, still active and, with
roles, allowed. Role-based access control
covers how the decision is made.
Secrets
Secrets belong to one function and are read at runtime, never written in the
code or in nulvless.jsonc:
nulvless secrets set STRIPE_SECRET_KEY --function checkoutWithout a value, the CLI asks for it without echoing it. Saving or removing a
secret redeploys the function with the new value. Names starting with
NULVLESS_ or NVL_, and EDGE_REGION, are reserved.
nulvless secrets pull --function checkout writes the values into
.env.local in the function's folder, readable only by you and kept out of git,
to run the function on your machine.
Versions and rollback
Every deploy is a version. When one goes wrong, serve an earlier one again without rebuilding it:
nulvless functions deployments checkout # newest first, the live one marked
nulvless functions promote checkout v2Nulvless keeps the live version and the three successful deploys before it.
The next deploy replaces the promoted version as usual.
Logs and limits
What the function writes with console.log, info, warn, error and
debug lands in the project's logs under Functions, with the matching
level, next to each request's status, latency and memory. The
Observability guide covers searching them.
Each request runs with a memory, CPU time and execution time limit. A request
that goes over one is stopped and answers with status 546.