创建 API 路由
当你创建 Postgres 表、视图或函数时,API 路由会自动生成。
🌐 API routes are automatically created when you create Postgres Tables, Views, or Functions.
创建一个表格 #
🌐 Create a table
通过创建一个名为 todos 的表来存储任务,从而创建你的第一个 API 路由。这会创建一个对应的路由 todos,可以接受 GET、POST、PATCH 和 DELETE 请求。
🌐 Create your first API route by creating a table called todos to store tasks.
This creates a corresponding route todos which can accept GET, POST, PATCH, & DELETE requests.
- 在仪表板中转到表格编辑器页面。
- 点击 新建表格 并创建一个名为
todos的表格。 - 点击保存。
- 点击 新建列 并创建一个名为
task、类型为text的列。 - 点击保存。
- 在仪表板的 集成 > 数据 API 部分,公开你想访问的特定表,如
todos,或你想使用的函数。要自动为public中的新表和新函数授予访问权限,请启用 新实体默认权限。
通过 API 暴露表或函数意味着什么
将权限(比如 select 或 execute)授予像 anon 或 authenticated 这样的角色,会让这些表或函数可以通过数据 API 访问。实际上,API 会检查你的 Postgres 权限——只有明确授予的对象会被暴露,其他所有访问默认都是拒绝的。
🌐 Granting privileges (like select or execute) to roles such as anon or authenticated makes those tables or functions accessible through the Data API. Behind the scenes, the API checks your Postgres permissions—only objects with explicit grants are exposed, and all other access is denied by default.
API URL 和密钥 #
🌐 API URL and keys
每个 Supabase 项目都有一个独特的 API URL。你的 API 通过 API 网关保护,每个请求都需要一个 API 密钥。
🌐 Every Supabase project has a unique API URL. Your API is secured behind an API gateway which requires an API Key for every request.
要做到这一点,你需要从项目的 Connect 对话框获取项目的 URL 和密钥。
🌐 To do this, you need to get the Project URL and key from the project's Connect dialog.
API 密钥的更改
Supabase 改变了密钥的工作方式,以提升项目安全性和开发者体验。你可以在 GitHub 上阅读完整公告。
🌐 Supabase has changed the way keys work to improve project security and developer experience. You can read the full announcement on GitHub.
它们将在2026年底被弃用,你现在应该改用可发布的(sb_publishable_xxx)和秘密的(sb_secret_xxx)密钥。
在大多数情况下,你可以从项目的 连接 对话框获得密钥,但如果你想要特定的密钥,可以在仪表板的设置 > API 密钥部分找到它们。
🌐 In most cases, you can get keys from the Project's Connect dialog, but if you want a specific key, you can find them in the Settings > API Keys section of the Dashboard.
- 对于新密钥,打开 API 密钥 标签,如果你还没有可发布的密钥,点击 创建新 API 密钥,然后从 可发布密钥 部分复制数值用于客户端操作。对于服务器端操作,则从 密钥 部分复制数值。
- 对于旧版密钥,从 Legacy API Keys 标签中复制
anon密钥用于客户端操作,service_role密钥用于服务器端操作。
阅读 API 密钥文档 以全面了解所有密钥类型及其用途。
可以通过 URL https://<project_ref>.supabase.co/rest/v1 访问 REST API
这两条路线都需要通过 apikey 头传递密钥。
🌐 Both of these routes require the key to be passed through an apikey header.
使用 API #
🌐 Using the API
你可以通过 HTTP 请求直接与 API 交互,或者使用我们提供的客户端库。
🌐 You can interact with your API directly via HTTP requests, or you can use the client libraries which we provide.
看看如何使用我们在第一步创建的 todos 表,使用我们提供的 API URL(SUPABASE_URL)和密钥(SUPABASE_PUBLISHABLE_KEY)来发出请求:
🌐 See how to make a request to the todos table which we created in the first step,
using the API URL (SUPABASE_URL) and Key (SUPABASE_PUBLISHABLE_KEY) we provided:
1// Initialize the JS client2import { createClient } from '@supabase/supabase-js'34const supabase = createClient(SUPABASE_URL, SUPABASE_PUBLISHABLE_KEY)56// Make a request7const { data: todos, error } = await supabase.from('todos').select('*')JS参考: select(), insert(), update(), upsert(), delete(), rpc()(调用Postgres函数)。
🌐 JS Reference: select(),
insert(),
update(),
upsert(),
delete(),
rpc() (call Postgres functions).