SupabaseClient
import { SupabaseClient } from "https://esm.sh/@supabase/supabase-js@2.99.2/dist/index.d.mts";Supabase Client.
An isomorphic Javascript client for interacting with Postgres.
§Type Parameters
§Constructors
Create a new client for use in the browser.
The unique Supabase URL which is supplied when you create a new project in your project dashboard.
The unique Supabase Key which is supplied when you create a new project in your project dashboard.
You can switch in between schemas. The schema needs to be on the list of exposed schemas inside Supabase.
Set to "true" if you want to automatically refresh the token before expiring.
Set to "true" if you want to automatically save the user session into local storage.
Set to "true" if you want to automatically detects OAuth grants in the URL and signs in the user.
Options passed along to realtime-js constructor.
Options passed along to the storage-js constructor.
A custom fetch implementation.
Any additional headers to send with each network request.
Creating a client
import { createClient } from '@supabase/supabase-js'
// Create a single supabase client for interacting with your database
const supabase = createClient('https://xyzcompany.supabase.co', 'publishable-or-anon-key')
With a custom domain
import { createClient } from '@supabase/supabase-js'
// Use a custom domain as the supabase URL
const supabase = createClient('https://my-custom-domain.com', 'publishable-or-anon-key')
With additional parameters
import { createClient } from '@supabase/supabase-js'
const options = {
db: {
schema: 'public',
},
auth: {
autoRefreshToken: true,
persistSession: true,
detectSessionInUrl: true
},
global: {
headers: { 'x-my-custom-header': 'my-app-name' },
},
}
const supabase = createClient("https://xyzcompany.supabase.co", "publishable-or-anon-key", options)
With custom schemas
import { createClient } from '@supabase/supabase-js'
const supabase = createClient('https://xyzcompany.supabase.co', 'publishable-or-anon-key', {
// Provide a custom schema. Defaults to "public".
db: { schema: 'other_schema' }
})
Custom fetch implementation
import { createClient } from '@supabase/supabase-js'
const supabase = createClient('https://xyzcompany.supabase.co', 'publishable-or-anon-key', {
global: { fetch: fetch.bind(globalThis) }
})
React Native options with AsyncStorage
import 'react-native-url-polyfill/auto'
import { createClient } from '@supabase/supabase-js'
import AsyncStorage from "@react-native-async-storage/async-storage";
const supabase = createClient("https://xyzcompany.supabase.co", "publishable-or-anon-key", {
auth: {
storage: AsyncStorage,
autoRefreshToken: true,
persistSession: true,
detectSessionInUrl: false,
},
});
React Native options with Expo SecureStore
import 'react-native-url-polyfill/auto'
import { createClient } from '@supabase/supabase-js'
import AsyncStorage from '@react-native-async-storage/async-storage';
import * as SecureStore from 'expo-secure-store';
import * as aesjs from 'aes-js';
import 'react-native-get-random-values';
// As Expo's SecureStore does not support values larger than 2048
// bytes, an AES-256 key is generated and stored in SecureStore, while
// it is used to encrypt/decrypt values stored in AsyncStorage.
class LargeSecureStore {
private async _encrypt(key: string, value: string) {
const encryptionKey = crypto.getRandomValues(new Uint8Array(256 / 8));
const cipher = new aesjs.ModeOfOperation.ctr(encryptionKey, new aesjs.Counter(1));
const encryptedBytes = cipher.encrypt(aesjs.utils.utf8.toBytes(value));
await SecureStore.setItemAsync(key, aesjs.utils.hex.fromBytes(encryptionKey));
return aesjs.utils.hex.fromBytes(encryptedBytes);
}
private async _decrypt(key: string, value: string) {
const encryptionKeyHex = await SecureStore.getItemAsync(key);
if (!encryptionKeyHex) {
return encryptionKeyHex;
}
const cipher = new aesjs.ModeOfOperation.ctr(aesjs.utils.hex.toBytes(encryptionKeyHex), new aesjs.Counter(1));
const decryptedBytes = cipher.decrypt(aesjs.utils.hex.toBytes(value));
return aesjs.utils.utf8.fromBytes(decryptedBytes);
}
async getItem(key: string) {
const encrypted = await AsyncStorage.getItem(key);
if (!encrypted) { return encrypted; }
return await this._decrypt(key, encrypted);
}
async removeItem(key: string) {
await AsyncStorage.removeItem(key);
await SecureStore.deleteItemAsync(key);
}
async setItem(key: string, value: string) {
const encrypted = await this._encrypt(key, value);
await AsyncStorage.setItem(key, encrypted);
}
}
const supabase = createClient("https://xyzcompany.supabase.co", "publishable-or-anon-key", {
auth: {
storage: new LargeSecureStore(),
autoRefreshToken: true,
persistSession: true,
detectSessionInUrl: false,
},
});
With a database query
import { createClient } from '@supabase/supabase-js'
const supabase = createClient('https://xyzcompany.supabase.co', 'public-anon-key')
const { data } = await supabase.from('profiles').select('*')
§Properties
Supabase Auth allows you to create and manage user sessions for access to data that is secured by access policies.
Supabase Functions allows you to deploy and invoke edge functions.
Supabase Storage allows you to manage user-generated content, such as photos or videos.
§Methods
Creates a Realtime channel with Broadcast, Presence, and Postgres Changes.
- The name of the Realtime channel.
- The options to pass to the Realtime channel.
Returns all Realtime channels.
Unsubscribes and removes all Realtime channels from Realtime client.
Unsubscribes and removes Realtime channel from Realtime client.
- The name of the Realtime channel.
Perform a function call.
- The function name to call
- The arguments to pass to the function call
- Named parameters
- When set to
true,datawill not be returned. Useful if you only need the count.
- When set to
true, the function will be called with read-only access mode.
- Count algorithm to use to count rows returned by the function. Only applicable for set-returning functions.
"exact": Exact but slow count algorithm. Performs a COUNT(*) under the
hood.
"planned": Approximated but fast count algorithm. Uses the Postgres
statistics under the hood.
"estimated": Uses exact count for low numbers and planned count for high
numbers.
Select a schema to query or perform an function (rpc) call.
The schema needs to be on the list of exposed schemas inside Supabase.
- The schema to query