Overview
Server functions let you run backend logic inside your Canvas app. Write TypeScript or JavaScript handlers that execute server-side with access to environment variables, a built-in KV store, andfetch — without managing infrastructure.
How it works
- Include server function source files in your deploy’s
source_filesmap - Files under
api/become HTTP endpoints at/functions/ - Canvas transpiles TypeScript → JavaScript via esbuild and executes your handler in a sandboxed async context
| Source file | Endpoint |
|---|---|
api/hello.ts | GET/POST /functions/hello |
api/search.js | GET/POST /functions/search |
api/data/export.ts | GET/POST /functions/data/export |
Writing a server function
Export a default function that receives a context object and returns a response:Context object
Your handler receives actx object with:
| Property | Type | Description |
|---|---|---|
ctx.req.method | string | HTTP method (GET, POST, etc.) |
ctx.req.path | string | Request path |
ctx.req.query | object | URL query parameters |
ctx.req.body | any | Parsed JSON body (for POST/PUT) |
ctx.req.headers | object | Request headers (excluding internal headers) |
ctx.env | object | App environment variables |
ctx.kv | object | Key-value store (see below) |
Return value
Return an object with:| Field | Type | Default | Description |
|---|---|---|---|
status | number | 200 | HTTP status code |
body | any | — | Response body (object → JSON, string → text) |
headers | object | {} | Custom response headers |
Key-value store
Every app has a built-in Redis-backed KV store, accessible in server functions viactx.kv:
| Method | Signature | Description |
|---|---|---|
get | kv.get(key: string): Promise<string | null> | Read a value |
set | kv.set(key: string, value: string, ttl?: number): Promise<void> | Write a value (optional TTL in seconds, default 30 days) |
del | kv.del(key: string): Promise<void> | Delete a key |
keys | kv.keys(): Promise<string[]> | List all keys for this app |
Calling the Mixpeek API
Usefetch to call the Mixpeek API through the Canvas proxy — credentials are injected automatically:
Deploying with source files
Include your server functions in thesource_files field when deploying:
Runtime logging
console.log, console.warn, console.error, and console.info calls in server functions are captured and sent to the runtime logs system. View them in Studio or query via API.
Limitations
- Execution timeout: 30 seconds per request
- Request body size: 1 MB max
- No filesystem access: Server functions run in a sandboxed context
- No npm imports: Only
fetchand thectxAPIs are available in the execution context - TypeScript only:
.tsand.jsfiles are supported (transpiled via esbuild)

