Skip to content
REST API

在不到两分钟内建立一个 API 路由。

Create your first API route by creating a public leaderboard table.

本指南讲解了如何创建一个 REST 路由,你可以使用 cURL 或浏览器进行查询,通过创建一个名为 leaderboard 的数据库表来存储玩家分数。这会创建一个对应的 API 路由 /rest/v1/leaderboard,可以接收 GETPOSTPATCHDELETE 请求。

🌐 This guide covers creating a REST route you can query using cURL or the browser by creating a database table called leaderboard to hold player scores. This creates a corresponding API route /rest/v1/leaderboard which can accept GET, POST, PATCH, and DELETE requests.

1
Set up a Supabase project with a 'leaderboard' table

在 Supabase 仪表板中创建一个新项目

在你的项目准备好之后,在你的 Supabase 数据库中创建一个表。你可以使用 表格编辑器SQL 编辑器 来操作。

🌐 After your project is ready, create a table in your Supabase database. You can do this with either the Table Editor or the SQL Editor.

1
-- Create a "leaderboard" table to store
2
-- player names and their scores.
3
create table leaderboard (
4
id serial primary key,
5
player text not null,
6
score integer not null default 0,
7
created_at timestamptz default now()
8
);
2
Enable Data API access to Anon Role

通过数据 API 公开 leaderboard 表,这样就可以通过 HTTP 查询。排行榜本来就是公开的,所以匿名客户端只需要读取权限。

想更好地控制哪些表和函数可以被访问,可以阅读 显式授权指南

1
-- Allow read-only access for anonymous clients
2
grant select on public.leaderboard to anon;
3
Configure RLS

为这个表启用行级安全(RLS)并创建控制谁可以读取和写入行的策略。对于排行榜,任何人都应该可以查看分数。只有经过认证的用户才能提交或更新分数。

1
-- Turn on RLS
2
alter table "leaderboard"
3
enable row level security;
4
5
-- Anyone can read the leaderboard
6
create policy "Leaderboard is public"
7
on leaderboard
8
for select
9
to anon, authenticated
10
using (true);
11
12
-- Authenticated users can submit and update scores
13
create policy "Authenticated users can submit scores"
14
on leaderboard
15
for insert
16
to authenticated
17
with check (true);
18
19
create policy "Authenticated users can update scores"
20
on leaderboard
21
for update
22
to authenticated
23
using (true)
24
with check (true);
4
Enable Data API access for authenticated and service roles

设置 RLS 后,给 authenticatedservice_role 角色写权限。

1
-- Grant write access only after RLS and policies are in place
2
grant select, insert, update, delete on public.leaderboard to authenticated;
3
grant select, insert, update, delete on public.leaderboard to service_role;
5
Insert some dummy data

现在往表里加一些分数,这样 API 才有东西可以查询。

1
insert into leaderboard (player, score)
2
values
3
('alice', 4200),
4
('bob', 3700),
5
('carol', 5100),
6
('dave', 2900);
6
Fetch the data

你可以在仪表板的 设置 > API 设置 部分找到你的 API URL 和密钥。通过在 API URL 后添加 /rest/v1/leaderboard 来查询 leaderboard 表。

复制这段代码,替换 <PROJECT_REF><PUBLISHABLE_KEY>,然后在终端运行它。

1
curl 'https://<PROJECT_REF>.supabase.co/rest/v1/leaderboard?select=*&order=score.desc' \
2
-H "apikey: <PUBLISHABLE_KEY>"

奖金 #

🌐 Bonus

有几种方式可以访问你的数据:

🌐 There are several options for accessing your data:

浏览器 #

🌐 Browser

你可以在浏览器中查询路线,只需将 publishable 键作为查询参数附加即可:

🌐 You can query the route in your browser, by appending the publishable key as a query parameter:

https://<PROJECT_REF>.supabase.co/rest/v1/leaderboard?apikey=<PUBLISHABLE_KEY>

大括号 #

🌐 Curl

1
curl 'https://<PROJECT_REF>.supabase.co/rest/v1/leaderboard?select=*&order=score.desc' \
2
-H "apikey: <PUBLISHABLE_KEY>" \

客户端库 #

🌐 Client libraries

我们提供了多种客户端库

🌐 We provide a number of Client Libraries.

1
const { data, error } = await supabase
2
.from('leaderboard')
3
.select()
4
.order('score', { ascending: false })