* fix: rewrite relative URLs when syncing to GitHub discussion Relative URLs back to supabse.com won't work in GitHub discussions, so rewrite them back to absolute URLs starting with https://supabase.com * fix: replace all supabase urls with relative urls * chore: add linting for relative urls * chore: bump linter version * Prettier --------- Co-authored-by: Chris Chinchilla <chris.ward@supabase.io>
114 lines
3.4 KiB
Plaintext
114 lines
3.4 KiB
Plaintext
---
|
|
id: 'creating-routes'
|
|
title: 'Creating API Routes'
|
|
description: '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.
|
|
|
|
## Create a table
|
|
|
|
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"
|
|
>
|
|
<TabPanel id="dashboard" label="Dashboard">
|
|
|
|
1. Go to the [Table editor](/dashboard/project/_/editor) page in the Dashboard.
|
|
1. Click **New Table** and create a table with the name `todos`.
|
|
1. Click **Save**.
|
|
1. Click **New Column** and create a column with the name `task` and type `text`.
|
|
1. Click **Save**.
|
|
|
|
<video width="99%" muted playsInline controls={true}>
|
|
<source
|
|
src="https://xguihxuzqibwxjnimxev.supabase.co/storage/v1/object/public/videos/docs/api/api-create-table-sm.mp4"
|
|
type="video/mp4"
|
|
/>
|
|
</video>
|
|
|
|
</TabPanel>
|
|
<TabPanel id="sql" label="SQL">
|
|
|
|
```sql
|
|
-- 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)
|
|
);
|
|
```
|
|
|
|
</TabPanel>
|
|
</Tabs>
|
|
|
|
## API URL and keys
|
|
|
|
Every Supabase project has a unique API URL. Your API is secured behind an API gateway which requires an API Key for every request.
|
|
|
|
1. Go to the [Settings](/dashboard/project/_/settings/general) page in the Dashboard.
|
|
2. Click **API** in the sidebar.
|
|
3. Find your API `URL`, `anon`, and `service_role` keys on this page.
|
|
|
|
<video width="99%" muted playsInline controls={true}>
|
|
<source
|
|
src="https://xguihxuzqibwxjnimxev.supabase.co/storage/v1/object/public/videos/docs/api/api-url-and-key.mp4"
|
|
type="video/mp4"
|
|
/>
|
|
</video>
|
|
|
|
The REST API is accessible through the URL `https://<project_ref>.supabase.co/rest/v1`
|
|
|
|
Both of these routes require the `anon` key to be passed through an `apikey` header.
|
|
|
|
## Using the API
|
|
|
|
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_PUBLISHABLE_KEY`) we provided:
|
|
|
|
<Tabs
|
|
scrollable
|
|
size="small"
|
|
type="underlined"
|
|
defaultActiveId="javascript"
|
|
queryGroup="language"
|
|
>
|
|
<TabPanel id="javascript" label="Javascript">
|
|
|
|
```javascript
|
|
// Initialize the JS client
|
|
import { createClient } from '@supabase/supabase-js'
|
|
const supabase = createClient(SUPABASE_URL, SUPABASE_PUBLISHABLE_KEY)
|
|
|
|
// Make a request
|
|
const { data: todos, error } = await supabase.from('todos').select('*')
|
|
```
|
|
|
|
</TabPanel>
|
|
<TabPanel id="curl" label="cURL">
|
|
|
|
```bash
|
|
# Append /rest/v1/ to your URL, and then use the table name as the route
|
|
curl '<SUPABASE_URL>/rest/v1/todos' \
|
|
-H "apikey: <SUPABASE_PUBLISHABLE_KEY>" \
|
|
-H "Authorization: Bearer <SUPABASE_PUBLISHABLE_KEY>"
|
|
```
|
|
|
|
</TabPanel>
|
|
</Tabs>
|
|
|
|
JS Reference: [`select()`](/docs/reference/javascript/select),
|
|
[`insert()`](/docs/reference/javascript/insert),
|
|
[`update()`](/docs/reference/javascript/update),
|
|
[`upsert()`](/docs/reference/javascript/upsert),
|
|
[`delete()`](/docs/reference/javascript/delete),
|
|
[`rpc()`](/docs/reference/javascript/rpc) (call Postgres functions).
|