Skip to content
REST API

创建 API 路由

当你创建 Postgres 表、视图或函数时,API 路由会自动生成。

🌐 API routes are automatically created when you create Postgres Tables, Views, or Functions.

创建一个表格 #

🌐 Create a table

通过创建一个名为 todos 的表来存储任务,从而创建你的第一个 API 路由。这会创建一个对应的路由 todos,可以接受 GETPOSTPATCHDELETE 请求。

🌐 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.

  1. 在仪表板中转到表格编辑器页面。
  2. 点击 新建表格 并创建一个名为 todos 的表格。
  3. 点击保存
  4. 点击 新建列 并创建一个名为 task、类型为 text 的列。
  5. 点击保存
  6. 在仪表板的 集成 > 数据 API 部分,公开你想访问的特定表,如 todos,或你想使用的函数。要自动为 public 中的新表和新函数授予访问权限,请启用 新实体默认权限

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 密钥文档 以全面了解所有密钥类型及其用途。

可以通过 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 client
2
import { createClient } from '@supabase/supabase-js'
3
4
const supabase = createClient(SUPABASE_URL, SUPABASE_PUBLISHABLE_KEY)
5
6
// Make a request
7
const { 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).