wix-cli-backend-api by wix/skills
npx skills add https://github.com/wix/skills --skill wix-cli-backend-api为 Wix CLI 应用程序创建 HTTP 端点 —— 这些是服务器端路由,用于处理 HTTP 请求、处理数据并返回响应。HTTP 端点由 Astro 端点提供支持,并能从文件系统自动发现。
关键信息:
src/pages/api/ 目录下,扩展名为 .tsnpm run generate 添加 —— 请直接创建文件在以下情况下使用 HTTP 端点:
文件路径决定了端点 URL:
src/pages/api/<your-endpoint-name>.ts
使用方括号表示动态参数:
src/pages/api/users/[id].ts → /api/users/:id
src/pages/api/posts/[slug].ts → /api/posts/:slug
src/pages/api/users/[userId]/posts/[postId].ts → /api/users/:userId/posts/:postId
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
为每个 HTTP 方法导出命名函数。使用来自 astro 的 APIRoute 进行类型定义。每个处理程序接收一个 request 对象并返回一个 Response:
import type { APIRoute } from "astro";
export const GET: APIRoute = async ({ request }) => {
console.log("Log from GET."); // 此消息会记录到你的 CLI。
return new Response("Response from GET."); // 此响应在浏览器控制台中可见
};
export const POST: APIRoute = async ({ request }) => {
const data = await request.json();
console.log("Log POST with body: ", data); // 此消息会记录到你的 CLI。
return new Response(JSON.stringify(data)); // 此响应在浏览器控制台中可见。
};
export const GET: APIRoute = async ({ params }) => {
const { id } = params; // 来自 /api/users/[id]
if (!id) {
return new Response(JSON.stringify({ error: "ID required" }), {
status: 400,
statusText: "Bad Request",
headers: { "Content-Type": "application/json" },
});
}
// 使用 id 获取数据
};
使用 new URL(request.url).searchParams:
export const GET: APIRoute = async ({ request }) => {
const url = new URL(request.url);
const search = url.searchParams.get("search");
const limit = parseInt(url.searchParams.get("limit") || "10", 10);
const offset = parseInt(url.searchParams.get("offset") || "0", 10);
// 使用查询参数
};
解析来自 POST/PUT/PATCH 请求的 JSON 请求体:
export const POST: APIRoute = async ({ request }) => {
try {
const body = await request.json();
const { title, content } = body;
if (!title || !content) {
return new Response(
JSON.stringify({ error: "Title and content required" }),
{
status: 400,
statusText: "Bad Request",
headers: { "Content-Type": "application/json" },
}
);
}
// 处理数据
} catch {
return new Response(JSON.stringify({ error: "Invalid JSON" }), {
status: 400,
statusText: "Bad Request",
headers: { "Content-Type": "application/json" },
});
}
};
const authHeader = request.headers.get("Authorization");
const contentType = request.headers.get("Content-Type");
始终返回一个带有适当状态码和响应头的 Response 对象:
// 200 OK
return new Response(JSON.stringify({ data: result }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
// 201 Created
return new Response(JSON.stringify({ id: newId, ...data }), {
status: 201,
headers: { "Content-Type": "application/json" },
});
// 204 No Content (用于 DELETE)
return new Response(null, { status: 204 });
// 400 Bad Request
return new Response(JSON.stringify({ error: "Invalid input" }), {
status: 400,
statusText: "Bad Request",
headers: { "Content-Type": "application/json" },
});
// 404 Not Found
return new Response(JSON.stringify({ error: "Not found" }), {
status: 404,
statusText: "Not Found",
headers: { "Content-Type": "application/json" },
});
// 500 Internal Server Error
return new Response(JSON.stringify({ error: "Internal server error" }), {
status: 500,
statusText: "Internal Server Error",
headers: { "Content-Type": "application/json" },
});
使用 Wix 内置的 HTTP 客户端 (httpClient.fetchWithAuth()) 从前端组件调用 HTTP 端点:
import { httpClient } from "@wix/essentials";
// GET 请求
const baseApiUrl = new URL(import.meta.url).origin;
const res = await httpClient.fetchWithAuth(
`${baseApiUrl}/api/<your-endpoint-name>`,
);
const data = await res.text();
// POST 请求
const res = await httpClient.fetchWithAuth(
`${baseApiUrl}/api/<your-endpoint-name>`,
{
method: "POST",
body: JSON.stringify({ message: "Hello from frontend" }),
},
);
const data = await res.json();
要将 HTTP 端点投入生产环境,请构建并发布你的项目:
发布后,端点即可通过生产环境 URL 访问并处理实时流量。
要删除 HTTP 端点,请删除 src/pages/api/ 下的文件并重新发布。
src/pages/api/
├── users.ts # /api/users 端点
├── users/
│ └── [id].ts # /api/users/:id 端点
└── posts.ts # /api/posts 端点
any,显式返回类型)astro 的 APIRoute 对所有处理程序进行类型定义JSON.stringify() 返回 Response 对象Content-Type: application/json 响应头statusText@ts-ignore 注释每周安装量
196
代码仓库
GitHub 星标数
3
首次出现
2026年1月26日
安全审计
安装于
opencode174
cursor95
codex90
gemini-cli88
github-copilot84
kimi-cli80
Creates HTTP endpoints for Wix CLI applications — server-side routes that handle HTTP requests, process data, and return responses. HTTP endpoints are powered by Astro endpoints and are automatically discovered from the file system.
Key facts:
src/pages/api/ with .ts extensionnpm run generate — create files directlyUse HTTP endpoints when you need to:
File path determines the endpoint URL:
src/pages/api/<your-endpoint-name>.ts
Use square brackets for dynamic parameters:
src/pages/api/users/[id].ts → /api/users/:id
src/pages/api/posts/[slug].ts → /api/posts/:slug
src/pages/api/users/[userId]/posts/[postId].ts → /api/users/:userId/posts/:postId
Export named functions for each HTTP method. Type with APIRoute from astro. Each handler receives a request object and returns a Response:
import type { APIRoute } from "astro";
export const GET: APIRoute = async ({ request }) => {
console.log("Log from GET."); // This message logs to your CLI.
return new Response("Response from GET."); // This response is visible in the browser console
};
export const POST: APIRoute = async ({ request }) => {
const data = await request.json();
console.log("Log POST with body: ", data); // This message logs to your CLI.
return new Response(JSON.stringify(data)); // This response is visible in the browser console.
};
export const GET: APIRoute = async ({ params }) => {
const { id } = params; // From /api/users/[id]
if (!id) {
return new Response(JSON.stringify({ error: "ID required" }), {
status: 400,
statusText: "Bad Request",
headers: { "Content-Type": "application/json" },
});
}
// Use id to fetch data
};
Use new URL(request.url).searchParams:
export const GET: APIRoute = async ({ request }) => {
const url = new URL(request.url);
const search = url.searchParams.get("search");
const limit = parseInt(url.searchParams.get("limit") || "10", 10);
const offset = parseInt(url.searchParams.get("offset") || "0", 10);
// Use query parameters
};
Parse JSON body from POST/PUT/PATCH requests:
export const POST: APIRoute = async ({ request }) => {
try {
const body = await request.json();
const { title, content } = body;
if (!title || !content) {
return new Response(
JSON.stringify({ error: "Title and content required" }),
{
status: 400,
statusText: "Bad Request",
headers: { "Content-Type": "application/json" },
}
);
}
// Process data
} catch {
return new Response(JSON.stringify({ error: "Invalid JSON" }), {
status: 400,
statusText: "Bad Request",
headers: { "Content-Type": "application/json" },
});
}
};
const authHeader = request.headers.get("Authorization");
const contentType = request.headers.get("Content-Type");
Always return a Response object with proper status codes and headers:
// 200 OK
return new Response(JSON.stringify({ data: result }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
// 201 Created
return new Response(JSON.stringify({ id: newId, ...data }), {
status: 201,
headers: { "Content-Type": "application/json" },
});
// 204 No Content (for DELETE)
return new Response(null, { status: 204 });
// 400 Bad Request
return new Response(JSON.stringify({ error: "Invalid input" }), {
status: 400,
statusText: "Bad Request",
headers: { "Content-Type": "application/json" },
});
// 404 Not Found
return new Response(JSON.stringify({ error: "Not found" }), {
status: 404,
statusText: "Not Found",
headers: { "Content-Type": "application/json" },
});
// 500 Internal Server Error
return new Response(JSON.stringify({ error: "Internal server error" }), {
status: 500,
statusText: "Internal Server Error",
headers: { "Content-Type": "application/json" },
});
Call HTTP endpoints from frontend components using Wix's built-in HTTP client (httpClient.fetchWithAuth()):
import { httpClient } from "@wix/essentials";
// GET request
const baseApiUrl = new URL(import.meta.url).origin;
const res = await httpClient.fetchWithAuth(
`${baseApiUrl}/api/<your-endpoint-name>`,
);
const data = await res.text();
// POST request
const res = await httpClient.fetchWithAuth(
`${baseApiUrl}/api/<your-endpoint-name>`,
{
method: "POST",
body: JSON.stringify({ message: "Hello from frontend" }),
},
);
const data = await res.json();
To take HTTP endpoints to production, build and release your project:
build command.preview command to share with team members for testing.release command.Once released, endpoints are accessible at production URLs and handle live traffic.
To delete an HTTP endpoint, remove the file under src/pages/api/ and release again.
src/pages/api/
├── users.ts # /api/users endpoint
├── users/
│ └── [id].ts # /api/users/:id endpoint
└── posts.ts # /api/posts endpoint
any, explicit return types)APIRoute from astroResponse objects with JSON.stringify() for JSONContent-Type: application/json header on JSON responsesstatusText in error responses@ts-ignore commentsWeekly Installs
196
Repository
GitHub Stars
3
First Seen
Jan 26, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode174
cursor95
codex90
gemini-cli88
github-copilot84
kimi-cli80
Lark Skill Maker 教程:基于飞书CLI创建AI技能,自动化工作流与API调用指南
27,800 周安装