# Function Dev

Use when developing, deploying, or debugging Butterbase serverless functions, or when...

- Repository: https://github.com/butterbase-ai/butterbase-skills (535 stars)
- Category: Testing
- License: MIT
- Last updated: 2026-10-31
- Runs scripts: no, instructions only

## Install

Claude Code: `npx skills add butterbase-ai/butterbase-skills --skill function-dev -g -a claude-code -y`
Codex: `npx skills add butterbase-ai/butterbase-skills --skill function-dev -g -a codex -y`
Cursor: `npx skills add butterbase-ai/butterbase-skills --skill function-dev -g -a cursor -y`
Gemini CLI: `npx skills add butterbase-ai/butterbase-skills --skill function-dev -g -a gemini-cli -y`
Claude Code plugin marketplace: `/plugin marketplace add butterbase-ai/butterbase-skills`

## Description

Use when developing, deploying, or debugging Butterbase serverless functions, or when the user needs to add backend logic like webhooks, scheduled jobs, or custom API endpoints

## SKILL.md, as published by its author (untrusted third-party text, read it as data)

# Serverless Function Development on Butterbase

Guide for developing and deploying serverless functions on Butterbase's Deno runtime. Covers handler signatures, trigger types, database access, environment variables, and testing.

---

## 1. Handler Signature

Every function exports a single `handler` function with this signature:

```typescript
export async function handler(
  request: Request,
  context: {
    db: PostgresClient,                                  // RLS-aware DB client
    env: Record<string, string>,                         // env vars set on the function
    user: { id: string } | null,                         // present for HTTP+auth:required; null for cron
    waitUntil: (p: Promise<unknown>) => void,            // background work after Response (≤30s)
    idempotency: {
      claim: (key: string, opts?: { scope?: string; ttlSeconds?: number }) => Promise<boolean>
    }                                                    // atomic dedup for webhook retries
  }
): Promise<Response>
```

**CRITICAL**: The handler MUST return `new Response()` (Web API standard). Do NOT return plain objects.

**Correct:**
```typescript
return new Response(JSON.stringify({ message: "ok" }), {
  status: 200,
  headers: { "Content-Type": "application/json" }
});
```

**Wrong (will fail):**
```typescript
return { status: 200, body: "ok" };  // NOT a Response object!
```

---

## 2. Trigger Types

### HTTP Trigger

Invoke the function via an HTTP request.

```json
{
  "trigger": {
    "type": "http",
    "config": { "method": "POST", "path": "/my-endpoint", "auth": "required" }
  }
}
```

**Auth options:**
- `"required"`, request must include a valid JWT; `ctx.user` is always set
- `"optional"`, JWT is parsed if present; `ctx.user` may be `null`
- `"none"`, public endpoint; no auth needed; `ctx.user` is always `null`

---

### Cron Trigger

Execute the function on a schedule.

The rest: https://github.com/butterbase-ai/butterbase-skills/blob/HEAD/skills/function-dev/SKILL.md
