Docs/ Backend as a Service/ Functions (Deno)

Functions (Deno)

Serverless TypeScript functions on your own server. Write, deploy, and invoke via HTTPS.

Creating a Function Server

To create a new function runtime:

  1. Navigate to Function Servers in the sh0 dashboard.
  2. Click Create Function Server.
  3. Enter a name (e.g., "my-api-functions").
  4. Click Create. sh0 deploys a Deno runtime instance with auto-SSL.

Each function server uses approximately 256 MB of memory and is accessible at https://<name>.sh0.app.

Writing Functions

Functions use the standard Web API pattern: a default export that receives a Request and returns a Response.

export default async function(req: Request) {
const url = new URL(req.url);
const name = url.searchParams.get('name') ?? 'world';
return new Response(
JSON.stringify({ hello: name }),
{ headers: { 'Content-Type': 'application/json' } },
);
}

Request Object

The Request object is the standard Web API Request. Access:

  • req.method -- HTTP method (GET, POST, etc.)
  • req.url -- full request URL
  • req.headers -- request headers
  • await req.json() -- parse JSON body
  • await req.text() -- raw body text

Response Object

Return a standard Web API Response. You can return any content type:

// JSON
return new Response(JSON.stringify(data), {
headers: { 'Content-Type': 'application/json' }
});
// HTML
return new Response('<h1>Hello</h1>', {
headers: { 'Content-Type': 'text/html' }
});
// Status codes
return new Response('Not found', { status: 404 });

Deploying

Deploy your function by uploading the TypeScript/JavaScript file via:

  • Dashboard -- paste or upload your function file in the function server detail page.
  • API -- POST the function code to the sh0 API endpoint for your function server.

Deno compiles and caches the function on the first request. Subsequent requests are served from the cache.

Tip
No build step is needed. Deno supports TypeScript natively. Just write .ts and deploy.

Invoking

Call your function at its HTTPS endpoint:

# GET request
curl https://my-api-functions.sh0.app/?name=sh0
# POST with JSON body
curl -X POST https://my-api-functions.sh0.app/ \
-H "Content-Type: application/json" \
-d '{\"action\": \"process\"}'

Using npm Packages

Deno supports importing npm packages directly using the npm: specifier:

import Stripe from "npm:stripe";
import { z } from "npm:zod";
export default async function(req: Request) {
const stripe = new Stripe(Deno.env.get('STRIPE_KEY')!);
// ...
}

You can also import from URLs (https://deno.land/x/...) or use import maps for cleaner imports.

Connecting to Databases

Your functions can connect to any database server managed by sh0. Use the internal Docker network hostname for fast, local connections:

import postgres from "npm:postgres";
const sql = postgres({
host: 'my-postgres-server', // Docker network name
port: 5432,
database: 'mydb',
username: 'user',
password: Deno.env.get('DB_PASSWORD'),
});
export default async function(req: Request) {
const users = await sql`SELECT * FROM users LIMIT 10`;
return new Response(JSON.stringify(users));
}
Warning
Store database credentials as environment variables, not hardcoded in your function. Configure them in the function server settings.