id | title | description |
---|---|---|
creating-routes |
Creating API Routes |
API routes are automatically created when you create Postgres Tables, Views, or Functions. |
API routes are automatically created when you create Postgres Tables, Views, or Functions.
Let's create our 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.
<Tabs scrollable size="small" type="underlined" defaultActiveId="dashboard" queryGroup="database-method"
- Go to the Table editor page in the Dashboard.
- Click New Table and create a table with the name
todos
. - Click Save.
- Click New Column and create a column with the name
task
and typetext
. - Click Save.
-- Create a table called "todos" with a column to store tasks.
create table
todos (
id bigint generated by default as identity primary key,
task text check (char_length(task) > 3)
);
Every Supabase project has a unique API URL. Your API is secured behind an API gateway which requires an API Key for every request.
- Go to the Settings page in the Dashboard.
- Click API in the sidebar.
- Find your API
URL
, , andservice_role
keys on this page.
The REST API is accessible through the URL https://<project_ref>.supabase.co/rest/v1
Both of these routes require the key to be passed through an apikey
header.
You can interact with your API directly via HTTP requests, or you can use the client libraries which we provide.
Let's 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_ANON_KEY
) we provided:
<Tabs scrollable size="small" type="underlined" defaultActiveId="javascript" queryGroup="language"
// Initialize the JS client
import { createClient } from '@supabase/supabase-js'
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY)
// Make a request
const { data: todos, error } = await supabase.from('todos').select('*')
# Append /rest/v1/ to your URL, and then use the table name as the route
curl '<SUPABASE_URL>/rest/v1/todos' \
-H "apikey: <SUPABASE_ANON_KEY>" \
-H "Authorization: Bearer <SUPABASE_ANON_KEY>"
JS Reference: select(), insert(), update(), upsert(), delete(), rpc() (call Postgres functions).