Home

Creating API Routes

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.

  1. Go to the Table editor page 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.

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 page in the Dashboard.
  2. Click API in the sidebar.
  3. Find your API URL, anon, and service_role keys on this page.

The REST API and the GraphQL API are both accessible through this URL:

  • REST: https://<project_ref>.supabase.co/rest/v1
  • GraphQL: https://<project_ref>.supabase.co/graphql/v1

Both of these routes require the anon key to be passed through an apikey header.

Using the API#

REST 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_ANON_KEY) we provided:


_10
// Initialize the JS client
_10
import { createClient } from '@supabase/supabase-js'
_10
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY)
_10
_10
// Make a request
_10
const { data: todos, error } = await supabase.from('todos').select('*')

JS Reference: select(), insert(), update(), upsert(), delete(), rpc() (call Postgres functions).

GraphQL API#

You can use any GraphQL client with the Supabase GraphQL API. For our GraphQL example we will use urql.


_38
import { createClient, useQuery } from 'urql'
_38
_38
// Prepare API key and Authorization header
_38
const headers = {
_38
apikey: <SUPABASE_ANON_KEY>,
_38
authorization: `Bearer ${<SUPABASE_ANON_KEY>}`,
_38
}
_38
_38
// Create GraphQL client
_38
// See: https://formidable.com/open-source/urql/docs/basics/react-preact/#setting-up-the-client
_38
const client = createClient({
_38
url: '<SUPABASE_URL>/graphql/v1',
_38
fetchOptions: function createFetchOptions() {
_38
return { headers }
_38
},
_38
})
_38
_38
// Prepare our GraphQL query
_38
const TodosQuery = `
_38
query {
_38
todosCollection {
_38
edges {
_38
node {
_38
id
_38
title
_38
}
_38
}
_38
}
_38
}
_38
`
_38
_38
// Query for the data (React)
_38
const [result, reexecuteQuery] = useQuery({
_38
query: TodosQuery,
_38
})
_38
_38
// Read the result
_38
const { data, fetching, error } = result

Realtime API#

By default Realtime is disabled on your database. Let's turn on Realtime for the todos table.

  1. Go to the Database page in the Dashboard.
  2. Click on Replication in the sidebar.
  3. Control which database events are sent by toggling Insert, Update, and Delete.
  4. Control which tables broadcast changes by selecting Source and toggling each table.

From the client, we can listen to any new data that is inserted into the todos table:


_11
// Initialize the JS client
_11
import { createClient } from '@supabase/supabase-js'
_11
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY)
_11
_11
// Create a function to handle inserts
_11
const handleInserts = (payload) => {
_11
console.log('Change received!', payload)
_11
}
_11
_11
// Listen to inserts
_11
const { data: todos, error } = await supabase.from('todos').on('INSERT', handleInserts).subscribe()

Use subscribe() to listen to database changes. The Realtime API works through PostgreSQL's replication functionality. Postgres sends database changes to a publication called supabase_realtime, and by managing this publication you can control which data is broadcast.