netlify-functions
/netlify-functions
编写 Netlify Serverless 函数的指南。在创建 API 端点、后台处理、计划任务,或任何使用 Netlify Functions 的服务端逻辑时使用。涵盖现代语法(默认导出 + Config)、TypeScript、路径路由、后台函数、计划函数、流式传输和方法路由。
- Author
- kael-odin ↗
- Repository
- codex-plugins-zh ↗
- Licence
- MIT
- Last repository change
- 25.10.2026
Assessment and security
- Files in this package
- 5
- Scripts
- 0
- HTML comments
- 0
- URLs in SKILL.md
- 0
No signals detected. Repository changed on 25.10.2026, without references to retired models or APIs.
MIT, open: use and modify while retaining author attribution.
What it does and when to use it
编写 Netlify Serverless 函数的指南。在创建 API 端点、后台处理、计划任务,或任何使用 Netlify Functions 的服务端逻辑时使用。涵盖现代语法(默认导出 + Config)、TypeScript、路径路由、后台函数、计划函数、流式传输和方法路由。
Original author description
编写 Netlify Serverless 函数的指南。在创建 API 端点、后台处理、计划任务,或任何使用 Netlify Functions 的服务端逻辑时使用。涵盖现代语法(默认导出 + Config)、TypeScript、路径路由、后台函数、计划函数、流式传输和方法路由。
How it works
- Modern Syntax
- File Structure
- Path Routing
- Method Routing
- Background Functions
- Scheduled Functions
- Streaming Responses
- Context Object
- Environment Variables
- Resource Limits
SKILL.md preview
---
name: netlify-functions
description: "编写 Netlify Serverless 函数的指南。在创建 API 端点、后台处理、计划任务,或任何使用 Netlify Functions 的服务端逻辑时使用。涵盖现代语法(默认导出 + Config)、TypeScript、路径路由、后台函数、计划函数、流式传输和方法路由。"
---
# Netlify Functions
## Modern Syntax
Always use the modern default export + Config pattern. Never use the legacy `exports.handler` or named `handler` export.
```typescript
import type { Context, Config } from "@netlify/functions";
export default async (req: Request, context: Context) => {
return new Response("Hello, world!");
};
export const config: Config = {
path: "/api/hello",
};
```
The handler receives a standard Web API `Request` and returns a `Response`. The second argument is a Netlify `Context` object.
## File Structure
Place functions in `netlify/functions/`:
```
netlify/functions/
_shared/ # Non-function shared code (underscore prefix)
auth.ts
db.ts
items.ts # -> /.netlify/functions/items (or custom path via config)
users/index.ts # -> /.netlify/functions/users
```
Use `.ts` or `.mts` extensions. If both `.ts` and `.js` exist with the same name, the `.js` file takes precedence.
## Path Routing
Define custom paths via the `config` export:
```typescript
export const config: Config = {
path: "/api/items", // Static path
// path: "/api/items/:id", // Path parameter
// path: ["/api/items", "/api/items/:id"], // Multiple paths
// excludedPath: "/api/items/special", // Excluded paths
// preferStatic: true, // Don't override static files
};
```
Without a `path` config, functions are available at `/.netlify/functions/{name}`. Setting a `path` makes the function available **only** at that path.
Access path parameters via `context.params`:
```typescript
// config: { path: "/api/items/:id" }
export default async (req: Request, context: Context) => {
const { id } = context.params;
// ...
};
```
## Method Routing
```typescript
export default async (req: Request, context: Context) => {
switch (req.method) {
case "GET": return handleGet(context.params.id);
case "POST": return handlePost(await req.json());
case "DELETE": return handleDelete(context.params.id);
default: return new Response("Method not allowed", { status: 405 });
}
};
export const config: Config = {
path: "/api/items/:id",
method: ["GET", "POST", "DELETE"],
};
```
## Background Functions
For long-running tasks (up to 15 minutes). The client receives an immediate `202` response; return values are ignored.
Name the file with a `-background` suffix:
```
netlify/functions/process-background.ts
```
Store results externally (Netlify Blobs, database) for later retrieval.
## Scheduled Functions
Run on a cron schedule (UTC timezone):
```typescript
export default async (req: Request) => {
const { next_run } = await req.json();
console.log("Next invocation at:", next_run);
};
export const config: Config = {
schedule: "@hourly", // or cron: "0 * * * *"
};
```
Shortcuts: `@yearly`, `@monthly`, `@weekly`, `@daily`, `@hourly`. Scheduled functions have a **30-second timeout** and only run on published deploys.
## Streaming Responses
Return a `ReadableStream` body for streamed responses (up to 20 MB):
```typescript
export default async (req: Request) => {
const stream = new ReadableStream({ /* ... */ });
return new Response(stream, {
headers: { "Content-Type": "text/event-stream" },
});
};
```
## Context Object
| Property | Description |
|---|---|
| `context.params` | Path parameters from config |
| `context.geo` | `{ city, country: {code, name}, latitude, longitude, subdivision, timezone, postalCode }` |
| `context.ip` | Client IP address |
| `context.cookies` | `.get()`, `.set()`, `.delete()` |
| `context.deploy` | `{ context, id, published }` |
| `context.site` | `{ id, name, url }` |
| `context.account.id` | Team account ID |
| `context.requestId` | Unique request ID |
| `context.waitUntil(promise)` | Extend execution after response is sent |
## Environment Variables
Use `Netlify.env` (not `process.env`) inside functions:
```typescript
const apiKey = Netlify.env.get("API_KEY");
```
## Resource Limits
| Resource | Limit |
|---|---|
| Synchronous timeout | 60 seconds |
| Background timeout | 15 minutes |
| Scheduled timeout | 30 seconds |
| Memory | 1024 MB |
| Buffered payload | 6 MB |
| Streamed payload | 20 MB |
## Framework Considerations
Frameworks with server-side capabilities (Astro, Next.js, Nuxt, SvelteKit, TanStack Start) typically generate their own serverless functions via adapters. You usually do not write raw Netlify Functions in these projects — the framework adapter handles server-side rendering and API routes. Write Netlify Functions directly when:
- Using a client-side-only framework (Vite + React SPA, vanilla JS)
- Adding background or scheduled tasks to any project
- Building standalone API endpoints outside the framework's routing
See the **netlify-frameworks** skill for adapter setup.
Files in this package (5)
- LICENSE.txt 11 KB
- SKILL.md 5 KB
- agents/openai.yaml 1 KB
- assets/netlify-small.svg 1 KB
- assets/netlify.png 3 KB
Versions
| Repository | Score | Licence |
|---|---|---|
| This page · kael-odin/codex-plugins-zh | 100 | MIT |
| netlify/context-and-tools | 100 | MIT |
| PracticalSwan/agent-skills | 100 | MIT |
| netlify/context-and-tools | 100 | MIT |
Identical content appears in 1 other GitHub folders. We show one copy with the highest score.
Database snapshot: 13.11.2026 · package version 8840af4a7c3b (SHA-256 digest of the entire package)
Downloads in the last 30 days: 2 · ranking
Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.
Similar skills
- 86devops/deploy-netlify
Netlify 部署方法论,使用 netlify CLI 部署前端应用、静态站点和 Serverless Functions - 100netlify-edge-functions
编写 Netlify Edge Functions 的指南。在构建中间件、基于地理位置的逻辑、请求/响应处理、身份验证检查、A/B 测试,或任何低延迟边缘计算场景时使用。涵盖 Deno 运行时、`context.next()` 中间件模式、地理位置,以及何时选择边缘函数而非 Serverless 函数。 - 95memstack-deployment-netlify-deploy
Use this skill when the user says 'deploy to Netlify', 'Netlify setup', 'netlify-deploy', or needs to deploy a static site or serverless functions to Netlify with build configuration and custom domains. Do NOT use for Railway, Vercel, or VPS deployments. - 100netlify-edge-functions
Write, route, cache, and debug Netlify Edge Functions, the Deno-based TypeScript or JavaScript handlers in netlify/edge-functions that run at the network edge in front of static files and serverless functions. Activate this skill whenever someone wants to add auth middleware or… - 100netlify-image-cdn
使用 Netlify Image CDN 进行图片优化和转换的指南。在提供优化图片、创建响应式图片标记、设置用户上传图片流水线,或配置图片转换时使用。涵盖 `/.netlify/images` 端点、查询参数、远程图片白名单、简洁 URL 重写,以及结合 Functions + Blobs 的上传组合方案。 - 100netlify-database
Zero-config Postgres for Netlify apps via @netlify/database — querying data from Functions/Edge Functions, writing schema migrations, setting up Drizzle ORM, local dev with netlify dev, database branches for deploy previews, and migrating an existing Postgres project onto…