mirror of
https://github.com/supabase/supabase.git
synced 2026-10-11 20:35:07 +03:00
## I have read the [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) file. YES ## What kind of change does this PR introduce? docs update <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Documentation** * Added a curl command example in the API quickstart guide, demonstrating how to query the todos endpoint with proper authentication headers for quick API testing. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
257 lines
6.5 KiB
Plaintext
257 lines
6.5 KiB
Plaintext
---
|
|
title: 'Build an API route in less than 2 minutes.'
|
|
subtitle: 'Create your first API route by creating a table called `todos` to store tasks.'
|
|
breadcrumb: 'API Quickstart'
|
|
hideToc: true
|
|
---
|
|
|
|
Let's create our first REST route which we can query using `cURL` or the browser.
|
|
|
|
We'll create a database table called `todos` for storing tasks. This creates a corresponding API route `/rest/v1/todos` which can accept `GET`, `POST`, `PATCH`, & `DELETE` requests.
|
|
|
|
<StepHikeCompact>
|
|
|
|
<StepHikeCompact.Step step={1}>
|
|
<StepHikeCompact.Details title="Set up a Supabase project with a 'todos' table">
|
|
|
|
[Create a new project](/dashboard) in the Supabase Dashboard.
|
|
|
|
After your project is ready, create a table in your Supabase database. You can do this with either the Table interface or the [SQL Editor](/dashboard/project/_/sql).
|
|
|
|
</StepHikeCompact.Details>
|
|
|
|
<StepHikeCompact.Code>
|
|
|
|
<Tabs
|
|
scrollable
|
|
size="small"
|
|
type="underlined"
|
|
defaultActiveId="sql"
|
|
queryGroup="database-method"
|
|
>
|
|
<TabPanel id="sql" label="SQL">
|
|
|
|
```sql
|
|
-- Create a table called "todos"
|
|
-- with a column to store tasks.
|
|
create table todos (
|
|
id serial primary key,
|
|
task text
|
|
);
|
|
```
|
|
|
|
</TabPanel>
|
|
<TabPanel id="dashboard" label="Dashboard">
|
|
|
|
1. Go to the [**Table editor**](/dashboard/project/_/editor) section in the Dashboard.
|
|
2. Click **New Table** and create a table with the name `todos`.
|
|
3. Click **Save**.
|
|
4. Click **New Column** and create a column with the name `task` and type `text`.
|
|
5. Click **Save**.
|
|
|
|
</TabPanel>
|
|
</Tabs>
|
|
|
|
</StepHikeCompact.Code>
|
|
|
|
</StepHikeCompact.Step>
|
|
|
|
<StepHikeCompact.Step step={2}>
|
|
<StepHikeCompact.Details title="Enable Data API access to Anon Role">
|
|
|
|
Expose the `todos` table through the Data API so it can be queried over HTTP.
|
|
|
|
For more control over which tables and functions are exposed, see [Expose specific tables and functions](/docs/guides/database/data-api#expose-specific-tables-and-functions-recommended).
|
|
|
|
</StepHikeCompact.Details>
|
|
|
|
<StepHikeCompact.Code>
|
|
|
|
<Tabs
|
|
scrollable
|
|
size="small"
|
|
type="underlined"
|
|
defaultActiveId="sql"
|
|
queryGroup="database-method"
|
|
>
|
|
<TabPanel id="sql" label="SQL">
|
|
|
|
```sql
|
|
-- Allow read-only access for anonymous clients
|
|
grant select on public.todos to anon;
|
|
```
|
|
|
|
</TabPanel>
|
|
<TabPanel id="dashboard" label="Dashboard">
|
|
|
|
Go to [**Integrations > Data API > Settings**](/dashboard/project/_/integrations/data_api/settings). Under **Exposed schemas**, make sure `public` is included, then under **Exposed tables**, toggle on access for the `todos` table.
|
|
|
|
</TabPanel>
|
|
</Tabs>
|
|
|
|
</StepHikeCompact.Code>
|
|
|
|
</StepHikeCompact.Step>
|
|
|
|
<StepHikeCompact.Step step={3}>
|
|
|
|
<StepHikeCompact.Details title="Configure RLS">
|
|
|
|
Turn on Row Level Security for this table and create the policies that control who can read and write rows.
|
|
|
|
</StepHikeCompact.Details>
|
|
|
|
<StepHikeCompact.Code>
|
|
|
|
```sql
|
|
-- Turn on RLS
|
|
alter table "todos"
|
|
enable row level security;
|
|
|
|
-- Allow anonymous access
|
|
create policy "Allow public access"
|
|
on todos
|
|
for select
|
|
to anon
|
|
using (true);
|
|
|
|
-- Allow authenticated users to read and modify todos
|
|
create policy "Allow authenticated users to manage todos"
|
|
on todos
|
|
for all
|
|
to authenticated
|
|
using (true)
|
|
with check (true);
|
|
```
|
|
|
|
</StepHikeCompact.Code>
|
|
|
|
</StepHikeCompact.Step>
|
|
|
|
<StepHikeCompact.Step step={4}>
|
|
<StepHikeCompact.Details title="Enable Data API access for authenticated and service roles">
|
|
|
|
Now that RLS is in place, grant write access to the `authenticated` and `service_role` roles.
|
|
|
|
</StepHikeCompact.Details>
|
|
|
|
<StepHikeCompact.Code>
|
|
|
|
```sql
|
|
-- Grant write access only after RLS and policies are in place
|
|
grant select, insert, update, delete on public.todos to authenticated;
|
|
grant select, insert, update, delete on public.todos to service_role;
|
|
```
|
|
|
|
</StepHikeCompact.Code>
|
|
|
|
</StepHikeCompact.Step>
|
|
|
|
<StepHikeCompact.Step step={5}>
|
|
<StepHikeCompact.Details title="Insert some dummy data">
|
|
|
|
Now we can add some data to our table which we can access through our API.
|
|
|
|
</StepHikeCompact.Details>
|
|
|
|
<StepHikeCompact.Code>
|
|
|
|
```sql
|
|
insert into todos (task)
|
|
values
|
|
('Create tables'),
|
|
('Enable security'),
|
|
('Add data'),
|
|
('Fetch data from the API');
|
|
```
|
|
|
|
</StepHikeCompact.Code>
|
|
|
|
</StepHikeCompact.Step>
|
|
|
|
<StepHikeCompact.Step step={6}>
|
|
<StepHikeCompact.Details title="Fetch the data">
|
|
|
|
Find your API URL and Keys in your Dashboard [API Settings](/dashboard/project/_/settings/api). You can now query your "todos" table by appending `/rest/v1/todos` to the API URL.
|
|
|
|
Copy this block of code, substitute `<PROJECT_REF>` and `<PUBLISHABLE_KEY>`, then run it from a terminal.
|
|
|
|
</StepHikeCompact.Details>
|
|
<StepHikeCompact.Code>
|
|
|
|
```bash Terminal
|
|
curl 'https://<PROJECT_REF>.supabase.co/rest/v1/todos' \
|
|
-H "apikey: <PUBLISHABLE_KEY>"
|
|
```
|
|
|
|
</StepHikeCompact.Code>
|
|
|
|
</StepHikeCompact.Step>
|
|
|
|
</StepHikeCompact>
|
|
|
|
## Bonus
|
|
|
|
There are several options for accessing your data:
|
|
|
|
### Browser
|
|
|
|
You can query the route in your browser, by appending the `publishable` key as a query parameter:
|
|
|
|
`https://<PROJECT_REF>.supabase.co/rest/v1/todos?apikey=<PUBLISHABLE_KEY>`
|
|
|
|
### Curl
|
|
|
|
```
|
|
curl 'https://<PROJECT_REF>.supabase.co/rest/v1/todos?select=*' \
|
|
-H "apikey: <PUBLISHABLE_KEY>" \
|
|
-H "Authorization: Bearer <PUBLISHABLE_KEY>"
|
|
```
|
|
|
|
### Client libraries
|
|
|
|
We provide a number of [Client Libraries](https://github.com/supabase/supabase#client-libraries).
|
|
|
|
<Tabs
|
|
scrollable
|
|
size="small"
|
|
type="underlined"
|
|
defaultActiveId="js"
|
|
queryGroup="language"
|
|
>
|
|
<TabPanel id="js" label="JavaScript">
|
|
|
|
```js
|
|
const { data, error } = await supabase.from('todos').select()
|
|
```
|
|
|
|
</TabPanel>
|
|
<$Show if="sdk:dart">
|
|
<TabPanel id="dart" label="Dart">
|
|
|
|
```dart
|
|
final data = await supabase.from('todos').select('*');
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:python">
|
|
<TabPanel id="python" label="Python">
|
|
|
|
```python
|
|
response = supabase.table('todos').select("*").execute()
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:swift">
|
|
<TabPanel id="swift" label="Swift">
|
|
|
|
```swift
|
|
let response = try await supabase.from("todos").select()
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
</Tabs>
|