Initializing
You can initialize a new Supabase client using the createClient()
method.
The Supabase client is your entrypoint to the rest of the Supabase functionality and is the easiest way to interact with everything we offer within the Supabase ecosystem.
Parameters#
supabaseUrlrequired
string
The unique Supabase URL which is supplied when you create a new project in your project dashboard.
supabaseKeyrequired
string
The unique Supabase Key which is supplied when you create a new project in your project dashboard.
SupabaseClientOptionsrequired
object
No description provided.
authoptional
object
No description provided.
autoRefreshTokenoptional
boolean
Automatically refreshes the token for logged in users.
detectSessionInUrloptional
boolean
Detect a session from the URL. Used for OAuth login callbacks.
persistSessionoptional
boolean
Whether to persist a logged in session to storage.
storageoptional
object
A storage provider. Used to store the logged in session.
storageKeyoptional
string
Optional key name used for storing tokens in local storage
Properties
dboptional
object
The Postgres schema which your tables belong to. Must be on the list of exposed schemas in Supabase. Defaults to 'public'.
schemaoptional
SchemaName
No description provided.
Properties
globaloptional
object
No description provided.
fetchoptional
Fetch
A custom
fetch
implementation.headersoptional
Record
Optional headers for initializing the client.
Properties
realtimeoptional
RealtimeClientOptions
Options passed to the realtime-js instance
Properties
Examples#
createClient()#
1import { createClient } from '@supabase/supabase-js' 2 3// Create a single supabase client for interacting with your database 4const supabase = createClient('https://xyzcompany.supabase.co', 'public-anon-key')
With additional parameters#
1import { createClient } from '@supabase/supabase-js'
2
3const options = {
4 db: {
5 schema: 'public',
6 },
7 auth: {
8 autoRefreshToken: true,
9 persistSession: true,
10 detectSessionInUrl: true
11 },
12 global: {
13 headers: { 'x-my-custom-header': 'my-app-name' },
14 },
15}
16const supabase = createClient("https://xyzcompany.supabase.co", "public-anon-key", options)
API schemas#
1import { createClient } from '@supabase/supabase-js'
2
3// Provide a custom schema. Defaults to "public".
4const supabase = createClient('https://xyzcompany.supabase.co', 'public-anon-key', {
5 db: { schema: 'other_schema' }
6})
By default the API server points to the public
schema. You can enable other database schemas within the Dashboard.
Go to Settings > API > Schema
and add the schema which you want to expose to the API.
Note: each client connection can only access a single schema, so the code above can access the other_schema
schema but cannot access the public
schema.
Custom fetch
implementation#
1import { createClient } from '@supabase/supabase-js'
2
3const supabase = createClient('https://xyzcompany.supabase.co', 'public-anon-key', {
4 global: { fetch: fetch.bind(globalThis) }
5})
supabase-js
uses the cross-fetch
library to make HTTP requests,
but an alternative fetch
implementation can be provided as an option.
This is most useful in environments where cross-fetch
is not compatible (for instance Cloudflare Workers).
React Native options#
1import { createClient } from '@supabase/supabase-js'
2import AsyncStorage from "@react-native-async-storage/async-storage";
3
4const supabase = createClient("https://xyzcompany.supabase.co", "public-anon-key", {
5 auth: {
6 storage: AsyncStorage,
7 autoRefreshToken: true,
8 persistSession: true,
9 detectSessionInUrl: false,
10 },
11});
For React Native we recommend using AsyncStorage
as the storage implementation for Supabase Auth.