Build an API route in less than 2 minutes.
Create your first API route by creating a table called `todos` to store tasks.
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.
Set up a Supabase project with a 'todos' table
Create a new project 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.
_10-- Create a table called "todos"_10-- with a column to store tasks._10create table todos (_10 id serial primary key,_10 task text_10);
Allow anonymous access
Let's turn on Row Level Security for this table and allow anonymous access.
_10-- Turn on security_10alter table "todos"_10enable row level security;_10_10-- Allow anonymous access_10create policy "Allow anonymous access"_10 on todos_10 for select_10 to anon_10 using (true);
Insert some dummy data
Now we can add some data to our table which we can access through our API.
_10insert into todos (task)_10values_10 ('Create tables'),_10 ('Enable security'),_10 ('Add data'),_10 ('Fetch data from the API');
Fetch the data
Find your API URL and Keys in your Dashboard API Settings. 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 <ANON_KEY>
, then run it from a terminal.
Bonus#
There are several options for accessing your data:
Browser#
You can query the route in your browser, by appending the anon
key as a query parameter:
https://<PROJECT_REF>.supabase.co/rest/v1/users?apikey=<ANON_KEY>
Client libraries#
We provide a number of Client Libraries.
_10const { data, error } = await supabase.from('todos').select()
GraphQL#
Every table can be accessed through the GraphQL API by switching /rest/v1
with /graphql/v1
.
_39import { createClient, useQuery } from 'urql'_39_39const URL = '<SUPABASE_URL>/graphql/v1'_39const ANON_KEY = '<SUPABASE_ANON_KEY>'_39_39// Prepare API key and Authorization header_39const headers = {_39 apikey: `${ANON_KEY}`,_39 authorization: `Bearer ${ANON_KEY}`,_39}_39_39const client = createClient({_39 url: URL,_39 fetchOptions: function createFetchOptions() {_39 return { headers }_39 },_39})_39_39// Prepare our GraphQL query_39const TodosQuery = `_39 query {_39 todosCollection {_39 edges {_39 node {_39 id_39 task_39 }_39 }_39 }_39 }_39`_39_39// Query for the data (React)_39const [result, reexecuteQuery] = useQuery({_39 query: TodosQuery,_39})_39_39// Read the result_39const { data, fetching, error } = result