The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
More refactoring of the Serveless API section
Copplea07e53387fd8
diff --git a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.tsindex 441c7ca2aab..0198df856e1 100644--- a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts+++ b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts@@ -556,15 +556,28 @@ export const api = {url: '/guides/api',items: [{ name: 'Overview', url: '/guides/api', items: [] },+ { name: 'Quickstart', url: '/guides/api/quickstart', items: [] },+ {+ name: 'Guides',+ url: '/guides/api',+ items: [+ { name: 'Creating API routes', url: '/guides/api/creating-routes', items: [] },+ { name: 'Securing your API', url: '/guides/api/securing-your-api', items: [] },+ {+ name: 'Querying joins and nested tables',+ url: '/guides/api/joins-and-nesting',+ items: [],+ },+ ],+ },{name: 'REST',url: undefined,items: [+ { name: 'Auto-generated Docs', url: '/guides/api/rest/auto-generated-docs', items: [] },{ name: 'Client Libraries', url: '/guides/api/rest/client-libs', items: [] },{ name: 'Generating Types', url: '/guides/api/rest/generating-types', items: [] },- { name: 'Auto-generated Docs', url: '/guides/api/rest/auto-generated-docs', items: [] },- { name: 'Joins and Nested Tables', url: '/guides/api/rest/joins-and-nesting', items: [] },- { name: 'Caching', url: '/guides/api/rest/caching', items: [] },+ { name: 'Caching responses', url: '/guides/api/rest/caching', items: [] },],},{diff --git a/apps/docs/pages/guides/api.mdx b/apps/docs/pages/guides/api.mdxindex 04e5ecb97db..e72f70211db 100644--- a/apps/docs/pages/guides/api.mdx+++ b/apps/docs/pages/guides/api.mdx@@ -10,11 +10,15 @@ export const meta = {Supabase auto-generates three types of API directly from your database schema.-- REST - interact with your database through a restful interface.-- Realtime - listen to database changes.-- GraphQL - [in beta](https://supabase.com/blog/pg-graphql).+- REST - connect to your database through a restful interface, directly from the browser.+- Realtime - for listening to database changes.+- GraphQL - a fast and auto-generate GraphQL API.-The APIs are:+All the APIs are auto-generated from your database and are designed to get you building as fast as possible, without writing a single line of code.++You can use them directly from the browser (two-tier architecture), or as a complement to your own API server (three-tier architecture).++## Features- **Instant and auto-generated.** <br />As you update your database the changes are immediately accessible through your API.- **Self documenting.** <br />Supabase generates documentation in the Dashboard which updates as you make database changes.@@ -97,13 +101,12 @@ This creates a corresponding route `todos` which can accept `GET`, `POST`, `PATC<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)-);-+ -- 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>@@ -314,7 +317,8 @@ By default Realtime is disabled on your database. Let's turn on Realtime for the<TabPanel id="sql" label="SQL">```sql-alter publication supabase_realtime add table todos;+alter+ publication supabase_realtime add table todos;```</TabPanel>@@ -340,65 +344,6 @@ Use [subscribe()](/docs/reference/javascript/subscribe) to listen to database chThe 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.-## API Security--### Securing your Routes--Your API is 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).-When you create a table in Postgres, Row Level Security is disabled by default. To enable RLS:--<Tabs- scrollable- size="small"- type="underlined"- defaultActiveId="dashboard"->-<TabPanel id="dashboard" label="Dashboard">--1. Go to the [Authentication](https://app.supabase.com/project/_/auth/users) page in the Dashboard.-2. Click on **Policies** in the sidebar.-3. Select **Enable RLS** to enable Row Level Security.--</TabPanel>-<TabPanel id="sql" label="SQL">--```sql-alter table todos enable row level security;-```--</TabPanel>-</Tabs>--### The `service_role` key--Never expose the `service_role` key in a browser or anywhere where a user can see it. This Key is designed to bypass Row Level Security - so it should only be used on a private server.--A common use case for the `service_role` key is to run 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--For all projects, by default, the Postgres extension [safeupdate](https://github.com/eradman/pg-safeupdate) is enabled for all queries coming from the API.-This ensures that any `delete()` or `update()` would fail if there are no accompanying filters provided.-To confirm that safeupdate is enabled for queries going through the API of your project, the following query could be run:--```sql-select usename, useconfig from pg_shadow where usename = 'authenticator';-```--The expected value for `useconfig` should be:--```sql-['session_preload_libraries=supautils, safeupdate']-```-export const Page = ({ children }) => <Layout meta={meta} children={children} />export default Pagediff --git a/apps/docs/pages/guides/api/creating-routes.mdx b/apps/docs/pages/guides/api/creating-routes.mdxnew file mode 100644index 00000000000..97ba52d0e1e--- /dev/null+++ b/apps/docs/pages/guides/api/creating-routes.mdx@@ -0,0 +1,13 @@+import Layout from '~/layouts/DefaultGuideLayout'++export const meta = {+ id: 'creating-routes',+ title: 'Creating API Routes',+ description: 'A description.',+}++Creating routes++export const Page = ({ children }) => <Layout meta={meta} children={children} />++export default Pagediff --git a/apps/docs/pages/guides/api/rest/joins-and-nesting.mdx b/apps/docs/pages/guides/api/joins-and-nesting.mdxsimilarity index 87%rename from apps/docs/pages/guides/api/rest/joins-and-nesting.mdxrename to apps/docs/pages/guides/api/joins-and-nesting.mdxindex f0a3abc4c98..a7f460c6691 100644--- a/apps/docs/pages/guides/api/rest/joins-and-nesting.mdx+++ b/apps/docs/pages/guides/api/joins-and-nesting.mdx@@ -2,7 +2,7 @@ import Layout from '~/layouts/DefaultGuideLayout'export const meta = {id: 'joins-and-nested-tables',- title: 'Joins and Nested tables',+ title: 'Querying Joins and Nested tables',description: 'A description.',video: 'https://www.youtube.com/v/7CqlTU9aOR4',}diff --git a/apps/docs/pages/guides/api/quickstart.mdx b/apps/docs/pages/guides/api/quickstart.mdxnew file mode 100644index 00000000000..bb0ac03dcaf--- /dev/null+++ b/apps/docs/pages/guides/api/quickstart.mdx@@ -0,0 +1,136 @@+import Layout from '~/layouts/DefaultGuideLayout'+import StepHikeCompact from '~/components/StepHikeCompact'++export const meta = {+ title: 'Query your database 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.++This creates a corresponding route `todos` which can accept `GET`, `POST`, `PATCH`, & `DELETE` requests.++<StepHikeCompact>++ <StepHikeCompact.Step step={1}>+ <StepHikeCompact.Details title="Set up a Supabase project with a 'todos' table">++ [Create a new project](https://app.supabase.com) 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](https://app.supabase.com/project/_/sql).++ </StepHikeCompact.Details>++ <StepHikeCompact.Code>++ <Tabs+ scrollable+ size="small"+ type="underlined"+ defaultActiveId="sql"+ >+ <TabPanel id="sql" label="SQL">++ ```sql+ -- Create a table called "todos"+ -- with a column to store tasks.+ create table todos (+ id serial primary key,+ task text+ );+ ```++ </TabPanel>+ <TabPanel id="dashboard" label="Dashboard">++ <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>+ </Tabs>++ </StepHikeCompact.Code>++ </StepHikeCompact.Step>++ <StepHikeCompact.Step step={2}>++ <StepHikeCompact.Details title="Allow anonymous access">++ Let's turn on Row Level Security for this table and allow anonymous access.++ </StepHikeCompact.Details>++ <StepHikeCompact.Code>++ ```sql+ -- Turn on security+ alter table "todos"+ enable row level security;++ -- Allow anonymous access+ create policy "Allow anonymous access"+ on todos+ to anon+ for select+ using (true);+ ```++ </StepHikeCompact.Code>++ </StepHikeCompact.Step>++ <StepHikeCompact.Step step={3}>+ <StepHikeCompact.Details title="Insert some dummy data">++ Now we can add some data to our table which we can access through our API.++ </StepHikeCompact.Details>++ <StepHikeCompact.Code>++ ```sql+ insert into todos (task)+ values+ ('Create tables'),+ ('Enable security'),+ ('Add data'),+ ('Fetch data from the API');+ ```++ </StepHikeCompact.Code>++ </StepHikeCompact.Step>++ <StepHikeCompact.Step step={4}>+ <StepHikeCompact.Details title="Fetch the data">++ Find your API URL and Keys in your Dashboard [API Settings](https://app.supabase.com/project/_/settings/api). 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.++ </StepHikeCompact.Details>+ <StepHikeCompact.Code>++ ```bash Terminal+ curl 'https://<PROJECT_REF>.supabase.co/rest/v1/todos' \+ -H "apikey: <ANON_KEY>" \+ -H "Authorization: Bearer <ANON_KEY>"+ ```++ </StepHikeCompact.Code>++ </StepHikeCompact.Step>++</StepHikeCompact>++export const Page = ({ children }) => <Layout meta={meta} children={children} hideToc={true} />++export default Pagediff --git a/apps/docs/pages/guides/api/securing-your-api.mdx b/apps/docs/pages/guides/api/securing-your-api.mdxnew file mode 100644index 00000000000..6942d6d390d--- /dev/null+++ b/apps/docs/pages/guides/api/securing-your-api.mdx@@ -0,0 +1,124 @@+import Layout from '~/layouts/DefaultGuideLayout'++export const meta = {+ id: 'securing-your-api',+ title: 'Securing your API',+ 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.+To control access to your data, you can use [Policies](/docs/guides/auth#policies).++## Enabling Row Level Security++When you create a table in Postgres, Row Level Security is disabled by default. To enable RLS:++<Tabs+ scrollable+ size="small"+ type="underlined"+ defaultActiveId="dashboard"+>+<TabPanel id="dashboard" label="Dashboard">++1. Go to the [Authentication](https://app.supabase.com/project/_/auth/users) page in the Dashboard.+2. Click on **Policies** in the sidebar.+3. Select **Enable RLS** to enable Row Level Security.++</TabPanel>+<TabPanel id="sql" label="SQL">++```sql+alter table+ todos enable row level security;+```++</TabPanel>+</Tabs>++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.+This ensures that `delete()` and `update()` requests will fail if there are no filters provided.+To confirm that safeupdate is enabled for API queries, run the following query:++```sql+select+ usename,+ useconfig+from+ pg_shadow+where+ usename = 'authenticator';+```++The expected value for `useconfig` should be:++```sql+['session_preload_libraries=supautils, safeupdate']+```++export const Page = ({ children }) => <Layout meta={meta} children={children} />++export default Page