创建 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 会在 2026 年底弃用 anon 和 service_role 密钥。请改用可发布的 (sb_publishable_xxx) 和秘密的 (sb_secret_xxx) 密钥。有关更改的原因,请参见 GitHub 上的公告。
🌐 Supabase is deprecating the anon and service_role keys by the end of 2026. Use the publishable (sb_publishable_xxx) and secret (sb_secret_xxx) keys instead. For the reasoning behind the change, see the announcement on GitHub.
在大多数情况下,你可以从你项目的连接对话框获取密钥。至于获取密钥的所有方式,包括 CLI 和管理 API,请参考查找你的密钥。
🌐 In most cases you can get keys from your project's Connect dialog. For every way to retrieve a key, including the CLI and the Management API, refer to Find your keys.
查看 API 密钥 以获取有关所有密钥类型及其用途的完整说明。
🌐 See API keys for a full explanation of all key types and their uses.
可以通过 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:
// Initialize the JS clientimport { createClient } from '@supabase/supabase-js'const supabase = createClient(SUPABASE_URL, SUPABASE_PUBLISHABLE_KEY)// Make a requestconst { 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).