Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

More cleanup

Copplecc664aa3b59e
diff --git a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts
index 0198df856e1..7824f3549fe 100644
--- a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts
+++ b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts
@@ -562,6 +562,7 @@ export const api = {
url: '/guides/api',
items: [
{ name: 'Creating API routes', url: '/guides/api/creating-routes', items: [] },
+ { name: 'How API Keys work', url: '/guides/api/api-keys', items: [] },
{ name: 'Securing your API', url: '/guides/api/securing-your-api', items: [] },
{
name: 'Querying joins and nested tables',
@@ -571,7 +572,7 @@ export const api = {
],
},
{
- name: 'REST',
+ name: 'REST & REALTIME',
url: undefined,
items: [
{ name: 'Auto-generated Docs', url: '/guides/api/rest/auto-generated-docs', items: [] },
@@ -583,7 +584,7 @@ export const api = {
{
name: 'GRAPHQL',
url: undefined,
- items: [{ name: 'GraphiQL', url: '/guides/api/graphql/graphiql', items: [] }],
+ items: [{ name: 'GraphiQL Documentation', url: '/guides/api/graphql/graphiql', items: [] }],
},
],
}
diff --git a/apps/docs/pages/guides/api.mdx b/apps/docs/pages/guides/api.mdx
index e72f70211db..8cd1bccf46b 100644
--- a/apps/docs/pages/guides/api.mdx
+++ b/apps/docs/pages/guides/api.mdx
@@ -62,288 +62,6 @@ Supabase provides a Realtime API using [Realtime](https://github.com/supabase/re
Realtime leverages PostgreSQL's built-in logical replication. You can manage your Realtime API simply by managing Postgres publications.
Go to your project's [Replication section](https://app.supabase.com/project/_/database/replication) to get started.
-## Getting started
-
-All APIs are auto-created from Database tables. After you have added tables or functions to your database, you can use the APIs provided.
-
-### Creating API Routes
-
-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"
->
-<TabPanel id="dashboard" label="Dashboard">
-
-1. Go to the [Table editor](https://app.supabase.com/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"
- muted
- playsInline
- />
-</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](https://app.supabase.com/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"
- muted
- playsInline
- />
-</video>
-
-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.
-
-#### API Keys
-
-You are provided with two keys:
-
-- an `anon` key, which is safe to be used in a browser context.
-- a `service_role` key, which should only be used on a server. This key can bypass Row Level Security. NEVER use this key in a browser.
-
-### Accessing the docs in the Dashboard
-
-#### REST API [#rest-api-dashboard-docs]
-
-Supabase generates documentation in the [Dashboard](https://app.supabase.com) which updates as you make database changes.
-Let's view the documentation for a `countries` table which we created in our database.
-
-1. Go to the [API](https://app.supabase.com/project/_/api) page in the Dashboard.
-2. Find the `countries` table under **Tables and Views** in the sidebar.
-3. Switch between the JavaScript and the cURL docs using the tabs.
-
-<video width="99%" muted playsInline controls="true">
- <source
- src="https://xguihxuzqibwxjnimxev.supabase.co/storage/v1/object/public/videos/docs/api/api-docs.mp4"
- type="video/mp4"
- muted
- playsInline
- />
-</video>
-
-#### GraphQL
-
-The GraphQL Endpoint that we provide (`https://<project_ref>.supabase.co/graphql/v1`) is compatible with any GraphiQL implementation that can pass an `apikey` header.
-Some suggested applications:
-
-- [paw.cloud](https://paw.cloud)
-- [insomnia.rest](https://insomnia.rest)
-- [postman.com/graphql](https://www.postman.com/graphql/)
-- Self-hosted GraphiQL: GraphiQL can be served through a simple HTML file. See [this discussion](https://github.com/supabase/supabase/discussions/6144) for more details.
-
-## 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:
-
-<Tabs
- scrollable
- size="small"
- type="underlined"
- defaultActiveId="javascript"
->
-<TabPanel id="javascript" label="Javascript">
-
-```javascript
-// 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('*')
-```
-
-</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_ANON_KEY>" \
--H "Authorization: Bearer <SUPABASE_ANON_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).
-
-### GraphQL API
-
-You can use any GraphQL client with the Supabase GraphQL API. For our GraphQL example we will use [urql](https://formidable.com/open-source/urql/docs/).
-
-<Tabs
- scrollable
- size="small"
- type="underlined"
- defaultActiveId="javascript"
->
-<TabPanel id="javascript" label="Javascript">
-
-```javascript
-import { createClient, useQuery } from 'urql'
-
-// Prepare API key and Authorization header
-const headers = {
- apikey: <SUPABASE_ANON_KEY>,
- authorization: `Bearer ${<SUPABASE_ANON_KEY>}`,
-}
-
-// Create GraphQL client
-// See: https://formidable.com/open-source/urql/docs/basics/react-preact/#setting-up-the-client
-const client = createClient({
- url: '<SUPABASE_URL>/graphql/v1',
- fetchOptions: function createFetchOptions() {
- return { headers }
- },
-})
-
-// Prepare our GraphQL query
-const TodosQuery = `
- query {
- todosCollection {
- edges {
- node {
- id
- title
- }
- }
- }
- }
-`
-
-// Query for the data (React)
-const [result, reexecuteQuery] = useQuery({
- query: TodosQuery,
-})
-
-// Read the result
-const { data, fetching, error } = result
-```
-
-</TabPanel>
-<TabPanel id="curl" label="cURL">
-
-```bash
-# Append /graphql/v1/ to your URL, and then use the table name as the route
-curl --request POST '<SUPABASE_URL>/graphql/v1' \
--H 'apikey: <SUPABASE_ANON_KEY>' \
--H 'Authorization: Bearer <SUPABASE_ANON_KEY>' \
--H 'Content-Type: application/json' \
--d '{ "query":"{ todos(first: 3) { edges { node { id } } } }" }'
-```
-
-</TabPanel>
-</Tabs>
-
-### Realtime API
-
-By default Realtime is disabled on your database. Let's turn on Realtime for the `todos` table.
-
-<Tabs
- scrollable
- size="small"
- type="underlined"
- defaultActiveId="dashboard"
->
-<TabPanel id="dashboard" label="Dashboard">
-
-1. Go to the [Database](https://app.supabase.com/project/_/database/tables) 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.
-
-<video width="99%" muted playsInline controls="true">
- <source
- src="https://xguihxuzqibwxjnimxev.supabase.co/storage/v1/object/public/videos/docs/api/api-realtime.mp4"
- type="video/mp4"
- muted
- playsInline
- />
-</video>
-
-</TabPanel>
-<TabPanel id="sql" label="SQL">
-
-```sql
-alter
- publication supabase_realtime add table todos;
-```
-
-</TabPanel>
-</Tabs>
-
-From the client, we can listen to any new data that is inserted into the `todos` table:
-
-```javascript
-// Initialize the JS client
-import { createClient } from '@supabase/supabase-js'
-const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY)
-
-// Create a function to handle inserts
-const handleInserts = (payload) => {
- console.log('Change received!', payload)
-}
-
-// Listen to inserts
-const { data: todos, error } = await supabase.from('todos').on('INSERT', handleInserts).subscribe()
-```
-
-Use [subscribe()](/docs/reference/javascript/subscribe) to listen to database changes.
-The Realtime API works through PostgreSQL's replication functionality. Postgres sends database changes to a [publication](/docs/guides/database/replication#publications)
-called `supabase_realtime`, and by managing this publication you can control which data is broadcast.
-
export const Page = ({ children }) => <Layout meta={meta} children={children} />
export default Page
diff --git a/apps/docs/pages/guides/api/api-keys.mdx b/apps/docs/pages/guides/api/api-keys.mdx
new file mode 100644
index 00000000000..3f9869586f5
--- /dev/null
+++ b/apps/docs/pages/guides/api/api-keys.mdx
@@ -0,0 +1,70 @@
+import Layout from '~/layouts/DefaultGuideLayout'
+
+export const meta = {
+ id: 'api-keys',
+ title: 'Understanding API Keys',
+ description: 'Securing your Serverless API with Postgres Row Level Security.',
+}
+
+Supabase provides two default keys when you create a project: an `anon` key, and a `service_role` key. You can find both keys in the [API Settings](https://app.supabase.com/project/_/settings/api).
+
+The Serverless APIs are designed to work with Postgres Row Level Security (RLS). These keys both map to Postgres roles. You can find an `anon` user and a `service_role` user in the [Roles](http://app.supabase.com/project/_/database/roles) section of the dashboard.
+
+The keys are both long-lived JWTs. If you decode these keys, you will see that they contain the "role", an "issued date", and an "expiry date" ~10 years in the future.
+
+```json
+{
+ "role": "anon",
+ "iat": 1625137684,
+ "exp": 1940713684
+}
+```
+
+## The `anon` key
+
+The `anon` key has very few privileges. You can use it in your [RLS policies](/docs/guides/auth/row-level-security) for "anonymous" access. For example, this policy will allow access to the `profiles` table:
+
+```sql
+create policy "Allow anonymous access" on profiles to anon for
+select
+ using (true);
+```
+
+And similarity for disallowing access:
+
+```sql
+create policy "Disallow anonymous access" on profiles to anon for
+select
+ using (false);
+```
+
+If you are using [Supabase Auth](/docs/guides/auth/overview), then the `anon` role will automatically update to `authenticated` once a user is logged in:
+
+```sql
+create policy "Allow access to authenticated users" on profiles to authenticated for
+select
+ using (true);
+```
+
+## The `service_role` key
+
+The "service_role" is a predefined Postgres role with elevated privileges, designed to perform various administrative and service-related tasks. It can bypass Row Level Security, so it should only be used on a private server.
+
+<Admonition type="caution">
+ Never expose the `service_role` key in a browser or anywhere where a user can see it.
+</Admonition>
+
+A common use case for the `service_role` key is running data analytics jobs on the backend. To support joins on user id, it is often useful to grant the service role read access to `auth.users` table.
+
+```sql
+grant
+select
+ on table auth.users to service_role;
+```
+
+We have [partnered with GitHub](https://github.blog/changelog/2022-03-28-supabase-is-now-a-github-secret-scanning-partner/) to scan for Supabase `service_role` keys pushed to public repositories.
+If they detect any keys with service_role privileges being pushed to GitHub, they will forward the API key to us, so that we can automatically revoke the detected secrets and notify you, protecting your data against malicious actors.
+
+export const Page = ({ children }) => <Layout meta={meta} children={children} />
+
+export default Page
diff --git a/apps/docs/pages/guides/api/creating-routes.mdx b/apps/docs/pages/guides/api/creating-routes.mdx
index 97ba52d0e1e..c48442f12d1 100644
--- a/apps/docs/pages/guides/api/creating-routes.mdx
+++ b/apps/docs/pages/guides/api/creating-routes.mdx
@@ -3,10 +3,249 @@ import Layout from '~/layouts/DefaultGuideLayout'
export const meta = {
id: 'creating-routes',
title: 'Creating API Routes',
- description: 'A description.',
+ description:
+ 'API routes are automatically created when you create Postgres Tables, Views, or Functions.',
}
-Creating 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.
+
+<Tabs
+ scrollable
+ size="small"
+ type="underlined"
+ defaultActiveId="dashboard"
+>
+<TabPanel id="dashboard" label="Dashboard">
+
+1. Go to the [Table editor](https://app.supabase.com/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"
+ muted
+ playsInline
+ />
+</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](https://app.supabase.com/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"
+ muted
+ playsInline
+ />
+</video>
+
+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:
+
+<Tabs
+ scrollable
+ size="small"
+ type="underlined"
+ defaultActiveId="javascript"
+>
+<TabPanel id="javascript" label="Javascript">
+
+```javascript
+// 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('*')
+```
+
+</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_ANON_KEY>" \
+-H "Authorization: Bearer <SUPABASE_ANON_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).
+
+### GraphQL API
+
+You can use any GraphQL client with the Supabase GraphQL API. For our GraphQL example we will use [urql](https://formidable.com/open-source/urql/docs/).
+
+<Tabs
+ scrollable
+ size="small"
+ type="underlined"
+ defaultActiveId="javascript"
+>
+<TabPanel id="javascript" label="Javascript">
+
+```javascript
+import { createClient, useQuery } from 'urql'
+
+// Prepare API key and Authorization header
+const headers = {
+ apikey: <SUPABASE_ANON_KEY>,
+ authorization: `Bearer ${<SUPABASE_ANON_KEY>}`,
+}
+
+// Create GraphQL client
+// See: https://formidable.com/open-source/urql/docs/basics/react-preact/#setting-up-the-client
+const client = createClient({
+ url: '<SUPABASE_URL>/graphql/v1',
+ fetchOptions: function createFetchOptions() {
+ return { headers }
+ },
+})
+
+// Prepare our GraphQL query
+const TodosQuery = `
+ query {
+ todosCollection {
+ edges {
+ node {
+ id
+ title
+ }
+ }
+ }
+ }
+`
+
+// Query for the data (React)
+const [result, reexecuteQuery] = useQuery({
+ query: TodosQuery,
+})
+
+// Read the result
+const { data, fetching, error } = result
+```
+
+</TabPanel>
+<TabPanel id="curl" label="cURL">
+
+```bash
+# Append /graphql/v1/ to your URL, and then use the table name as the route
+curl --request POST '<SUPABASE_URL>/graphql/v1' \
+-H 'apikey: <SUPABASE_ANON_KEY>' \
+-H 'Authorization: Bearer <SUPABASE_ANON_KEY>' \
+-H 'Content-Type: application/json' \
+-d '{ "query":"{ todos(first: 3) { edges { node { id } } } }" }'
+```
+
+</TabPanel>
+</Tabs>
+
+### Realtime API
+
+By default Realtime is disabled on your database. Let's turn on Realtime for the `todos` table.
+
+<Tabs
+ scrollable
+ size="small"
+ type="underlined"
+ defaultActiveId="dashboard"
+>
+<TabPanel id="dashboard" label="Dashboard">
+
+1. Go to the [Database](https://app.supabase.com/project/_/database/tables) 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.
+
+<video width="99%" muted playsInline controls="true">
+ <source
+ src="https://xguihxuzqibwxjnimxev.supabase.co/storage/v1/object/public/videos/docs/api/api-realtime.mp4"
+ type="video/mp4"
+ muted
+ playsInline
+ />
+</video>
+
+</TabPanel>
+<TabPanel id="sql" label="SQL">
+
+```sql
+alter
+ publication supabase_realtime add table todos;
+```
+
+</TabPanel>
+</Tabs>
+
+From the client, we can listen to any new data that is inserted into the `todos` table:
+
+```javascript
+// Initialize the JS client
+import { createClient } from '@supabase/supabase-js'
+const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY)
+
+// Create a function to handle inserts
+const handleInserts = (payload) => {
+ console.log('Change received!', payload)
+}
+
+// Listen to inserts
+const { data: todos, error } = await supabase.from('todos').on('INSERT', handleInserts).subscribe()
+```
+
+Use [subscribe()](/docs/reference/javascript/subscribe) to listen to database changes.
+The Realtime API works through PostgreSQL's replication functionality. Postgres sends database changes to a [publication](/docs/guides/database/replication#publications)
+called `supabase_realtime`, and by managing this publication you can control which data is broadcast.
export const Page = ({ children }) => <Layout meta={meta} children={children} />
diff --git a/apps/docs/pages/guides/api/graphql/graphiql.mdx b/apps/docs/pages/guides/api/graphql/graphiql.mdx
index 9887e72278f..a3ce41073fd 100644
--- a/apps/docs/pages/guides/api/graphql/graphiql.mdx
+++ b/apps/docs/pages/guides/api/graphql/graphiql.mdx
@@ -7,7 +7,15 @@ export const meta = {
video: 'https://www.youtube.com/v/7CqlTU9aOR4',
}
-GraphiQL
+Every Supabase project has a GraphQL Endpoint: `https://<project_ref>.supabase.co/graphql/v1`.
+
+This endpoint is compatible with any GraphiQL implementation that can pass an `apikey` header.
+Some suggested applications:
+
+- [paw.cloud](https://paw.cloud)
+- [insomnia.rest](https://insomnia.rest)
+- [postman.com/graphql](https://www.postman.com/graphql/)
+- Self-hosted GraphiQL: GraphiQL can be served through a simple HTML file. See [this discussion](https://github.com/supabase/supabase/discussions/6144) for more details.
export const Page = ({ children }) => <Layout meta={meta} children={children} />
diff --git a/apps/docs/pages/guides/api/joins-and-nesting.mdx b/apps/docs/pages/guides/api/joins-and-nesting.mdx
index a7f460c6691..a5fe6f41231 100644
--- a/apps/docs/pages/guides/api/joins-and-nesting.mdx
+++ b/apps/docs/pages/guides/api/joins-and-nesting.mdx
@@ -3,11 +3,203 @@ import Layout from '~/layouts/DefaultGuideLayout'
export const meta = {
id: 'joins-and-nested-tables',
title: 'Querying Joins and Nested tables',
- description: 'A description.',
- video: 'https://www.youtube.com/v/7CqlTU9aOR4',
+ description: 'The Serverless APIs automatically detect relationships between Postgres tables.',
}
-Joins and nested tables.
+The Serverless APIs automatically detect relationships between Postgres tables. Since Postgres is a relational database, this is a very common scenario.
+
+## One-to-many joins
+
+Let's use an example database that stores `countries` and `cities`:
+
+<Tabs
+ scrollable
+ size="small"
+ type="underlined"
+ defaultActiveId="table"
+>
+<TabPanel id="table" label="Tables">
+
+**Countries**
+
+| `id` | `name` |
+| ---- | -------------- |
+| 1 | United Kingdom |
+| 2 | United States |
+
+**Cities**
+
+| `id` | `name` | `country_id` |
+| ---- | ----------- | ------------ |
+| 1 | London | 1 |
+| 2 | Manchester | 1 |
+| 3 | Los Angeles | 2 |
+| 4 | New York | 2 |
+
+</TabPanel>
+<TabPanel id="SQL" label="SQL">
+
+```sql
+create table
+ countries ("id" serial primary key, "name" text);
+
+insert into
+ countries (id, name)
+values
+ (1, 'United Kingdom'),
+ (2, 'United States');
+
+create table
+ cities (
+ "id" serial primary key,
+ "name" text,
+ "country_id" int references "countries"
+ );
+
+insert into
+ cities (name, country_id)
+values
+ ('London', 1),
+ ('Manchester', 1),
+ ('Los Angeles', 2),
+ ('New York', 2);
+```
+
+</TabPanel>
+</Tabs>
+
+The APIs will automatically detect relationships based on the foreign keys:
+
+<Tabs
+ scrollable
+ size="small"
+ type="underlined"
+ defaultActiveId="js"
+>
+<TabPanel id="js" label="JavaScript">
+
+```js
+const { data, error } = await supabase.from('countries').select(`
+ id,
+ name,
+ cities ( id, name )
+`)
+```
+
+</TabPanel>
+<TabPanel id="dart" label="Dart">
+
+```dart
+final data = await supabase.from('todos').select('id, name, cities(id, name)');
+```
+
+</TabPanel>
+<TabPanel id="graphql" label="GraphQL">
+
+```javascript
+const Query = `
+ query {
+ countriesCollection {
+ edges {
+ node {
+ id
+ name
+ cities {
+ id,
+ name
+ }
+ }
+ }
+ }
+ }
+`
+```
+
+</TabPanel>
+<TabPanel id="url" label="URL">
+
+```bash
+GET https://[REF].supabase.co/rest/v1/countries?select=id,name,cities(id,name)
+```
+
+</TabPanel>
+</Tabs>
+
+## Many-to-many joins
+
+The Serverless APIs will detect many-to-many joins. For example, if you have a database which stored teams of users (where each user could below to many teams):
+
+```sql
+create table
+ users ("id" serial primary key, "name" text);
+
+create table
+ teams ("id" serial primary key, "team_name" text);
+
+create table
+ members (
+ "id" serial primary key,
+ "user_id" int references users,
+ "team_id" int references teams
+ );
+```
+
+In these cases you don't need to explicity define the joining table (members). If we wanted to fetch all the teams and the members in each team:
+
+<Tabs
+ scrollable
+ size="small"
+ type="underlined"
+ defaultActiveId="js"
+>
+<TabPanel id="js" label="JavaScript">
+
+```js
+const { data, error } = await supabase.from('teams').select(`
+ id,
+ team_name,
+ users ( id, name )
+`)
+```
+
+</TabPanel>
+<TabPanel id="dart" label="Dart">
+
+```dart
+final data = await supabase.from('teams').select('id, team_name, users(id, name)');
+```
+
+</TabPanel>
+<TabPanel id="graphql" label="GraphQL">
+
+```javascript
+const Query = `
+ query {
+ teamsCollection {
+ edges {
+ node {
+ id
+ team_name
+ users {
+ id,
+ name
+ }
+ }
+ }
+ }
+ }
+`
+```
+
+</TabPanel>
+<TabPanel id="url" label="URL">
+
+```bash
+GET https://[REF].supabase.co/rest/v1/teams?select=id,team_name,users(id,name)
+```
+
+</TabPanel>
+</Tabs>
export const Page = ({ children }) => <Layout meta={meta} children={children} />
diff --git a/apps/docs/pages/guides/api/quickstart.mdx b/apps/docs/pages/guides/api/quickstart.mdx
index bb0ac03dcaf..7d645a8b5f7 100644
--- a/apps/docs/pages/guides/api/quickstart.mdx
+++ b/apps/docs/pages/guides/api/quickstart.mdx
@@ -2,14 +2,14 @@ import Layout from '~/layouts/DefaultGuideLayout'
import StepHikeCompact from '~/components/StepHikeCompact'
export const meta = {
- title: 'Query your database in less than 2 minutes.',
+ 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',
}
-Let's create our 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.
-This creates a corresponding route `todos` which can accept `GET`, `POST`, `PATCH`, & `DELETE` requests.
+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>
@@ -131,6 +131,106 @@ This creates a corresponding route `todos` which can accept `GET`, `POST`, `PATC
</StepHikeCompact>
+## 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 numerous [Client Libraries](https://github.com/supabase/supabase#client-libraries).
+
+<Tabs
+ scrollable
+ size="small"
+ type="underlined"
+ defaultActiveId="js"
+>
+<TabPanel id="js" label="JavaScript">
+
+```js
+const { data, error } = await supabase.from('todos').select()
+```
+
+</TabPanel>
+<TabPanel id="dart" label="Dart">
+
+```dart
+final data = await supabase.from('todos').select('*');
+```
+
+</TabPanel>
+<TabPanel id="python" label="Python">
+
+```python
+response = supabase.table('todos').select("*").execute()
+```
+
+</TabPanel>
+</Tabs>
+
+### GraphQL
+
+Every table can be accessed through the GraphQL API by switching `/rest/v1` with `/graphql/v1`.
+
+<Tabs
+ scrollable
+ size="small"
+ type="underlined"
+ defaultActiveId="urql"
+>
+<TabPanel id="urql" label="urql">
+
+```javascript
+import { createClient, useQuery } from 'urql'
+
+const URL = '<SUPABASE_URL>/graphql/v1'
+const ANON_KEY = '<SUPABASE_ANON_KEY>'
+
+// Prepare API key and Authorization header
+const headers = {
+ apikey: `${ANON_KEY}`,
+ authorization: `Bearer ${ANON_KEY}`,
+}
+
+const client = createClient({
+ url: URL,
+ fetchOptions: function createFetchOptions() {
+ return { headers }
+ },
+})
+
+// Prepare our GraphQL query
+const TodosQuery = `
+ query {
+ todosCollection {
+ edges {
+ node {
+ id
+ task
+ }
+ }
+ }
+ }
+`
+
+// Query for the data (React)
+const [result, reexecuteQuery] = useQuery({
+ query: TodosQuery,
+})
+
+// Read the result
+const { data, fetching, error } = result
+```
+
+</TabPanel>
+</Tabs>
+
export const Page = ({ children }) => <Layout meta={meta} children={children} hideToc={true} />
export default Page
diff --git a/apps/docs/pages/guides/api/rest/auto-generated-docs.mdx b/apps/docs/pages/guides/api/rest/auto-generated-docs.mdx
index bde5bb7bb21..82709aa7a1a 100644
--- a/apps/docs/pages/guides/api/rest/auto-generated-docs.mdx
+++ b/apps/docs/pages/guides/api/rest/auto-generated-docs.mdx
@@ -3,11 +3,23 @@ import Layout from '~/layouts/DefaultGuideLayout'
export const meta = {
id: 'auto-docs',
title: 'Auto-generated documentation',
- description: 'Suapbase provides several client libraries for the REST interface.',
- video: 'https://www.youtube.com/v/7CqlTU9aOR4',
+ description: 'Supabase provides documentation that updates automatically.',
}
-A doc on the Client Libraries, how they are structured, and where to find the docs.
+Supabase generates documentation in the [Dashboard](https://app.supabase.com) which updates as you make database changes.
+
+1. Go to the [API](https://app.supabase.com/project/_/api) page in the Dashboard.
+2. Select any table under **Tables and Views** in the sidebar.
+3. Switch between the JavaScript and the cURL docs using the tabs.
+
+<video width="99%" muted playsInline controls="true">
+ <source
+ src="https://xguihxuzqibwxjnimxev.supabase.co/storage/v1/object/public/videos/docs/api/api-docs.mp4"
+ type="video/mp4"
+ muted
+ playsInline
+ />
+</video>
export const Page = ({ children }) => <Layout meta={meta} children={children} />
diff --git a/apps/docs/pages/guides/api/securing-your-api.mdx b/apps/docs/pages/guides/api/securing-your-api.mdx
index 6942d6d390d..a1747d75943 100644
--- a/apps/docs/pages/guides/api/securing-your-api.mdx
+++ b/apps/docs/pages/guides/api/securing-your-api.mdx
@@ -6,7 +6,7 @@ export const meta = {
description: 'Securing your Serverless API with Postgres Row Level Security.',
}
-The Serverless APIs are designed to work with Postgres Row Level Security (RLS). If you use Supabase [Auth](/docs/guides/auth), you can restrict data based on the logged-in user.
+The Serverless APIs are designed to work with Postgres Row Level Security (RLS). If you use [Supabase Auth](/docs/guides/auth), you can restrict data based on the logged-in user.
To control access to your data, you can use [Policies](/docs/guides/auth#policies).
## Enabling Row Level Security
@@ -38,65 +38,6 @@ alter table
With RLS enabled, you can create Policies that allow or disallow users to access and update data. We provide a detailed guide for creating Row Level Security Policies in our [Authorization documentation](/docs/guides/auth/row-level-security).
-## API Keys
-
-Supabase provides two default keys when you create a project: an `anon` key, and a `service_role` key. You can find both keys in the [API Settings](https://app.supabase.com/project/_/settings/api).
-
-These keys both map to Postgres roles - you can find an `anon` user and a `service_role` user in the [Roles](http://app.supabase.com/project/_/database/roles) section of the dashboard. The keys are both long-lived JWTs. If you decode these keys, you will see that they contain the "role", an "issued date", and an "expiry date" in the future.
-
-```json
-{
- "role": "anon",
- "iat": 1625137684,
- "exp": 1940713684
-}
-```
-
-### The `anon` key
-
-The `anon` key has very few privileges. You can use it in your [RLS policies](/docs/guides/auth/row-level-security) for "anonymous" access. For example, this policy will allow access to the `profiles` table:
-
-```sql
-create policy "Allow anonymous access" on profiles to anon for
-select
- using (true);
-```
-
-And similarity for disallowing access:
-
-```sql
-create policy "Disallow anonymous access" on profiles to anon for
-select
- using (false);
-```
-
-If you are using [Supabase Auth](/docs/guides/auth/overview), then the `anon` role will automatically update to `authenticated` once a user is logged in:
-
-```sql
-create policy "Allow access to authenticated users" on profiles to authenticated for
-select
- using (true);
-```
-
-### The `service_role` key
-
-The "service_role" is a predefined Postgres role with elevated privileges, designed to perform various administrative and service-related tasks. It can bypass Row Level Security, so it should only be used on a private server.
-
-<Admonition type="caution">
- Never expose the `service_role` key in a browser or anywhere where a user can see it.
-</Admonition>
-
-A common use case for the `service_role` key is running data analytics jobs on the backend. To support joins on user id, it is often useful to grant the service role read access to `auth.users` table.
-
-```sql
-grant
-select
- on table auth.users to service_role;
-```
-
-We have [partnered with GitHub](https://github.blog/changelog/2022-03-28-supabase-is-now-a-github-secret-scanning-partner/) to scan for Supabase `service_role` keys pushed to public repositories.
-If they detect any keys with service_role privileges being pushed to GitHub, they will forward the API key to us, so that we can automatically revoke the detected secrets and notify you, protecting your data against malicious actors.
-
## Safeguards towards accidental deletes and updates
By default, all projects have the [safeupdate](https://github.com/eradman/pg-safeupdate) Postgres extension enabled for API queries.