swift Client Library
supabase-swiftView on GitHubThis reference documents every object and method available in Supabase's Swift library, supabase-swift. You can use supabase-swift to interact with your Postgres database, listen to database changes, invoke Deno Edge Functions, build login and user management functionality, and manage large files.
Installing
Install using Swift Package Manager
You can install Supabase package using Swift Package Manager.
The package exposes multiple libraries, you can choose between adding all of them using Supabase, or some of:
Auth
Realtime
Postgrest
Functions
Storage
_21let package = Package(_21 ..._21 dependencies: [_21 ..._21 .package(_21 url: "https://github.com/supabase/supabase-swift.git",_21 from: "2.0.0"_21 ),_21 ],_21 targets: [_21 .target(_21 name: "YourTargetName",_21 dependencies: [_21 .product(_21 name: "Supabase", // Auth, Realtime, Postgrest, Functions, or Storage_21 package: "supabase-swift"_21 ),_21 ]_21 )_21 ]_21)
Initializing
You can initialize Supabase with the SupabaseClient
by passing your Project URL
and Project Key
. You can find these under your Project Settings
→ API Settings
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.
_10let client = SupabaseClient(supabaseURL: URL(string: "https://xyzcompany.supabase.co")!, supabaseKey: "public-anon-key")
Fetch data
- By default, Supabase projects will return a maximum of 1,000 rows. This setting can be changed in Project API Settings. It's recommended that you keep it low to limit the payload size of accidental or malicious requests. You can use
range()
queries to paginate through your data. select()
can be combined with Modifiersselect()
can be combined with Filters- If using the Supabase hosted platform
apikey
is technically a reserved keyword, since the API gateway will pluck it out for authentication. It should be avoided as a column name. - The recommended solution for getting data is to use the value property which will return a decoded model. Create a
Codable
to easily decode your database responses.
_10struct Country: Decodable {_10 let id: Int_10 let name: String_10}_10_10let countries: [Country] = try await supabase_10 .from("countries")_10 .select()_10 .execute()_10 .value
Insert data
_11struct Country: Encodable {_11 let id: Int_11 let name: String_11}_11_11let country = Country(id: 1, name: "Denmark")_11_11try await supabase_11 .from("countries")_11 .insert(country)_11 .execute()
Update data
update()
should always be combined with Filters to target the item(s) you wish to update.
_10try await supabase_10 .from("countries")_10 .update(["name": "Australia"])_10 .eq("id", value: 1)_10 .execute()
Upsert data
- Primary keys must be included in
values
to use upsert.
_10struct Country: Encodable {_10 let id: Int_10 let name: String_10}_10try await supabase_10 .from("countries")_10 .upsert(Country(id: 1, name: "Albania"))_10 .execute()
Delete data
delete()
should always be combined with filters to target the item(s) you wish to delete.- If you use
delete()
with filters and you have RLS enabled, only rows visible throughSELECT
policies are deleted. Note that by default no rows are visible, so you need at least oneSELECT
/ALL
policy that makes the rows visible.
_10try await supabase_10 .from("countries")_10 .delete()_10 .eq("id", value: 1)_10 .execute()
Call a Postgres function
You can call Postgres functions as Remote Procedure Calls, logic in your database that you can execute from anywhere. Functions are useful when the logic rarely changes—like for password resets and updates.
_10create or replace function hello_world() returns text as $$_10 select 'Hello world';_10$$ language sql;
_10let value: String = try await supabase_10 .rpc("hello_world")_10 .execute()_10 .value
Using filters
Filters allow you to only return rows that match certain conditions.
Filters can be used on select()
, update()
, upsert()
, and delete()
queries.
If a Postgres function returns a table response, you can also apply filters.
Implement URLQueryRepresentable
protocol in your own types to be able to use them as filter value.
Supported filtes are: eq
, neq
, gt
, gte
, lt
, lte
, like
, ilike
, is
, in
, cs
, cd
, sl
, sr
, nxl
, nxr
, adj
, ov
, fts
, plfts
, phfts
, wfts
. Check available operators in PostgREST.
_10try await supabase_10 .from("cities")_10 .select("name, country_id")_10 .eq("name", value: "The Shire") // Correct_10_10try await supabase_10 .from("citites")_10 .eq("name", value: "The Shire") // Incorrect_10 .select("name, country_id")
Match an associated value
_10try await supabase_10 .from("countries")_10 .select("name")_10 .match(["id": 2, "name": "Albania"])
Don't match the filter
Finds all rows that don't satisfy the filter.
-
.not()
expects you to use the raw PostgREST syntax for the filter names and values._10.not("name", operator: .eq, value: "Paris")_10.not("arraycol", operator: .cs, value: #"{"a","b"}"#) // Use Postgres array {} for array column and 'cs' for contains._10.not("rangecol", operator: .cs, value: "(1,2]") // Use Postgres range syntax for range column._10.not("id", operator: .in, value: "(6,7)") // Use Postgres list () and 'in' for in_ filter._10.not("id", operator: .in, value: "(\(mylist.join(separator: ",")))") // You can insert a Swift list array.
_10try await supabase_10 .from("countries")_10 .select()_10 .not("name", operator: .is, value: "")_10 .execute()
Match at least one filter
or() expects you to use the raw PostgREST syntax for the filter names and values.
_10.or(#"id.in.(5,6,7), arraycol.cs.{"a","b"}"#) // Use `()` for `in` filter, `{}` for array values and `cs` for `contains()`._10.or(#"id.in.(5,6,7), arraycol.cd.{"a","b"}"#) // Use `cd` for `containedBy()`
_10try await supabase_10 .from("countries")_10 .select("name")_10 .or("id.eq.2,name.eq.Algeria")
Match the filter
filter() expects you to use the raw PostgREST syntax for the filter values.
_10.filter("id", operator: .in, value: "(5,6,7)") // Use `()` for `in` filter_10.filter("arraycol", operator: .cs, value: #"{"a","b"}"#) // Use `cs` for `contains()`, `{}` for array values
_10try await supabase_10 .from("countries")_10 .select()_10 .filter("name", operator: .in, value: #"("Algeria","Japan")"#)
Using modifiers
Filters work on the row level—they allow you to return rows that only match certain conditions without changing the shape of the rows. Modifiers are everything that don't fit that definition—allowing you to change the format of the response (e.g. returning a CSV string).
Modifiers must be specified after filters. Some modifiers only apply for queries that return rows (e.g., select()
or rpc()
on a function that returns a table response).
Return data after inserting
Perform a SELECT on the query result.
_10 try await supabase_10 .from("countries")_10 .upsert(CountryModel(id: 1, name: "Algeria"))_10 .select()_10 .execute()
Order the results
Order the query result by column.
_10try await supabase_10 .from("countries")_10 .select("id, name")_10 .order("id", ascending: false)_10 .execute()
Limit the number of rows returned
Limit the query result by count.
_10try await supabase_10 .from("countries")_10 .select("id, name")_10 .limit(1)_10 .execute()
Limit the query to a range
Limit the query result by from and to inclusively.
_12try await supabase_12 .from("countries")_12 .select(_12 """_12 name,_12 cities (_12 name_12 )_12 """_12 )_12 .range(from: 0, to: 1)_12 .execute()
Retrieve one row of data
By default PostgREST returns all JSON results in an array, even when there is only one item, use single()
to return the first object unenclosed by an array.
_10try await supabase_10 .from("countries")_10 .select("name")_10 .limit(1)_10 .single()_10 .execute()
Retrieve as a CSV
_10try await supabase_10 .from("countries")_10 .select()_10 .csv()_10 .execute()
Using explain
For debugging slow queries, you can get the Postgres EXPLAIN
execution plan of a query using the explain()
method. This works on any query, even for rpc()
or writes.
Explain is not enabled by default as it can reveal sensitive information about your database. It's best to only enable this for testing environments but if you wish to enable it for production you can provide additional protection by using a pre-request
function.
Follow the Performance Debugging Guide to enable the functionality on your project.
_10try await supabase_10 .from("countries")_10 .select()_10 .explain()_10 .execute()_10 .value
Overview
The auth methods can be accessed via the supabase.auth
namespace.
Handling deep links
UIKit app lifecycle
_29public func application(_29 _ application: UIApplication,_29 didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?_29) -> Bool {_29 if let url = launchOptions?[.url] as? URL {_29 supabase.auth.handle(url)_29 }_29_29 return true_29}_29_29func application(_29 _ app: UIApplication,_29 open url: URL,_29 options: [UIApplication.OpenURLOptionsKey: Any]_29) -> Bool {_29 supabase.auth.handle(url)_29 return true_29}_29_29#### UIKit app lifecycle with scenes_29_29In your `SceneDelegate.swift`:_29_29```swift_29func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {_29 guard let url = URLContexts.first?.url else { return }_29 supabase.auth.handle(url)_29}
SwiftUI app lifecycle
In your AppDelegate.swift
:
_10SomeView()_10 .onOpenURL { url in_10 supabase.auth.handle(url)_10 }
_10let supabase = SupabaseClient(supabaseURL: URL(string: "https://xyzcompany.supabase.co")!, supabaseKey: "public-anon-key")_10let auth = supabase.auth
Create a new user
- By default, the user needs to verify their email address before logging in. To turn this off, disable Confirm email in your project.
- Confirm email determines if users need to confirm their email address after signing up.
- If Confirm email is enabled, a
user
is returned butsession
is null. - If Confirm email is disabled, both a
user
and asession
are returned.
- If Confirm email is enabled, a
- When the user confirms their email address, they are redirected to the
SITE_URL
by default. You can modify yourSITE_URL
or add additional redirect URLs in your project. - If signUp() is called for an existing confirmed user:
- When both Confirm email and Confirm phone (even when phone provider is disabled) are enabled in your project, an obfuscated/fake user object is returned.
- When either Confirm email or Confirm phone (even when phone provider is disabled) is disabled, the error message,
User already registered
is returned.
- To fetch the currently logged-in user, refer to
getUser()
.
Parameters
- emailOptionalString
One of
email
orphone
must be provided. - phoneOptionalString
One of
email
orphone
must be provided. - passwordRequiredString
- dataOptionalJSONObject
A custom data object to store additional user metadata.
- redirectToOptionalURL
Only for email signups. The redirect URL embedded in the email link. Must be a configured redirect URL for your Supabase instance.
- captchaTokenOptionalString
_10try await supabase.auth.signUp(_10 email: "example@email.com",_10 password: "example-password"_10)
Listen to auth events
- Subscribes to important events occurring on the user's session.
- Emitted events:
INITIAL_SESSION
- Emitted right after the Supabase client is constructed and the initial session from storage is loaded.
SIGNED_IN
- Emitted each time a user session is confirmed or re-established, including on user sign in.
- Avoid making assumptions as to when this event is fired, this may occur even when the user is already signed in. Instead, check the user object attached to the event to see if a new user has signed in and update your application's UI.
SIGNED_OUT
- Emitted when the user signs out. This can be after:
- A call to
supabase.auth.signOut()
. - After the user's session has expired for any reason:
- User has signed out on another device.
- The session has reached its timebox limit or inactivity timeout.
- User has signed in on another device with single session per user enabled.
- Check the User Sessions docs for more information.
- A call to
- Use this to clean up any local storage your application has associated with the user.
- Emitted when the user signs out. This can be after:
TOKEN_REFRESHED
- Emitted each time a new access and refresh token are fetched for the signed in user.
- It's best practice and highly recommended to extract the access token (JWT) and store it in memory for further use in your application.
- Avoid frequent calls to
supabase.auth.session
for the same purpose.
- Avoid frequent calls to
- There is a background process that keeps track of when the session should be refreshed so you will always receive valid tokens by listening to this event.
- The frequency of this event is related to the JWT expiry limit configured on your project.
USER_UPDATED
- Emitted each time the
supabase.auth.update(user:)
method finishes successfully. Listen to it to update your application's UI based on new profile information.
- Emitted each time the
PASSWORD_RECOVERY
- Emitted instead of the
SIGNED_IN
event when the user lands on a page that includes a password recovery link in the URL. - Use it to show a UI to the user where they can reset their password.
- Emitted instead of the
_13_13// Using AsyncStream_13for await (event, session) in await supabase.auth.authStateChanges {_13 print(event, session)_13}_13_13// Using Closure_13let subscription = await supabase.auth.onAuthStateChange { event, session in _13 print(event, session)_13}_13_13// call remove() to remove subscription_13subscription.remove()
Create an anonymous user
- Returns an anonymous user
- It is recommended to set up captcha for anonymous sign-ins to prevent abuse. You can pass in the captcha token in the
options
param.
_10let session = try await supabase.auth.signInAnonymously(captchaToken: captchaToken)
Sign in a user
- Requires either an email and password or a phone number and password.
Parameters
- emailOptionalString
One of
email
orphone
must be provided. - phoneOptionalString
One of
email
orphone
must be provided. - passwordRequiredString
- captchaTokenOptionalString
_10try await supabase.auth.signIn(_10 email: "example@email.com",_10 password: "example-password"_10)
Sign in with ID Token
_10let session = try await supabase.auth.signInWithIdToken(_10 credentials: OpenIDConnectCredentials(_10 provider: .apple,_10 idToken: "your-id-token",_10 nonce: "your nonce"_10 )_10)
Sign in a user through OTP
- Requires either an email or phone number.
- This method is used for passwordless sign-ins where a OTP is sent to the user's email or phone number.
- If the user doesn't exist,
signInWithOTP()
will signup the user instead. To restrict this behavior, you can setshouldCreateUser
to `false``. - If you're using an email, you can configure whether you want the user to receive a magiclink or a OTP.
- If you're using phone, you can configure whether you want the user to receive a OTP.
- The magic link's destination URL is determined by the
SITE_URL
. - See redirect URLs and wildcards to add additional redirect URLs to your project.
- Magic links and OTPs share the same implementation. To send users a one-time code instead of a magic link, modify the magic link email template to include
{{ .Token }}
instead of{{ .ConfirmationURL }}
. - See our Twilio Phone Auth Guide for details about configuring WhatsApp sign in.
Parameters
- emailOptionalString
One of
email
orphone
must be provided. - phoneOptionalString
One of
email
orphone
must be provided. - redirectToOptionalString
Only for email signups. The redirect URL embedded in the email link. Must be a configured redirect URL for your Supabase instance.
- channelOptionalMessagingChannel
The channel to use for sending messages. Only for phone signups.
- shouldCreateUserOptionalBool
Whether to create the user if they don't already exist. Defaults to true.
- dataOptionalJSONObject
A custom data object to store additional user metadata.
- captchaTokenOptionalString
_10try await supabase.auth.signInWithOTP(_10 email: "example@email.com",_10 redirectTo: URL(string: "my-app-scheme://")!_10)
Sign in a user through OAuth
- This method is used for signing in using a third-party provider.
- Supabase supports many different third-party providers.
Parameters
- providerRequiredProvider
The third-party provider.
- redirectToOptionalURL
A URL to send the user to after they are confirmed.
- scopesOptionalString
A space-separated list of scopes granted to the OAuth application.
- queryParamsOptional[(name: String, value: String?)]
Additional query params.
- configureOptionalCallback
A custom configuration callback for opening the OAuth flow externally.
_10let session = try await supabase.auth.signInWithOAuth(_10 provider: .github_10) { (session: ASWebAuthenticationSession) in _10 // customize session_10}
Sign in a user through SSO
- Before you can call this method you need to establish a connection to an identity provider. Use the CLI commands to do this.
- If you've associated an email domain to the identity provider, you can use the
domain
property to start a sign-in flow. - In case you need to use a different way to start the authentication flow with an identity provider, you can use the
providerId
property. For example:- Mapping specific user email addresses with an identity provider.
- Using different hints to identity the identity provider to be used by the user, like a company-specific page, IP address or other tracking information.
Parameters
- providerIdOptionalString
UUID of the SSO provider. One of
providerId
ordomain
is required. - domainOptionalString
Domain name of the organization to use SSO with. One of
providerId
ordomain
is required. - redirectToRequiredString
The URL to redirect the user to after they have signed in. Must be a configured redirect URL for your Supabase instance.
- captchaTokenRequiredString
_10 // You can extract the user's email domain and use it to trigger the_10 // authentication flow with the correct identity provider._10_10 let url = try await await supabase.auth.signInWithSSO{_10 domain: "company.com"_10 }_10_10 // Open the URL using your preferred method to complete sign-in process._10 UIApplication.shared.open(url)
Sign out a user
- In order to use the
signOut()
method, the user needs to be signed in first.
_10try await supabase.auth.signOut()
Verify and log in through OTP
- The
verifyOTP
method takes in different verification types. If a phone number is used, the type can either besms
orphone_change
. If an email address is used, the type can be one of the following:signup
,magiclink
,recovery
,invite
,email_change
, oremail
. - The verification type used should be determined based on the corresponding auth method called before
verifyOTP
to sign up / sign-in a user.
Parameters
- emailOptionalString
One of
phone
,email
, ortoken_hash
must be provided. - phoneOptionalString
One of
phone
,email
, ortoken_hash
must be provided. - token_hashOptionalString
The token hash from the user's email link. One of
phone
,email
, ortoken_hash
must be provided. - typeRequiredEmailOTPType | MobileOTPType
- tokenOptionalString
The OTP sent to the user. Required if using
phone
oremail
. - redirectToOptionalURL
A URL to redirect the user to after they are confirmed. Must be in your configured redirect URLs.
- captchaTokenOptionalString
Deprecated.
_10try await supabase.auth.verifyOTP(_10 phone: "+13334445555",_10 token: "123456",_10 type: .sms_10)
Retrieve a session
- Returns the session, refreshing it if necessary. If no session can be found, a
GoTrueError.sessionNotFound
error is thrown.
_10try await supabase.auth.session
Retrieve a new session
- This method will refresh the session whether the current one is expired or not.
_10let session = try await supabase.auth.refreshSession()
Retrieve a user
- This method is useful for checking if the user is authorized because it validates the user's access token JWT on the server.
- Fetches the user object from the database instead of local session.
- Should be used only when you require the most current user data. For faster results,
session.user
is recommended.
_10let user = try await supabase.auth.user()
Update a user
- In order to use the
updateUser()
method, the user needs to be signed in first. - By default, email updates sends a confirmation link to both the user's current and new email. To only send a confirmation link to the user's new email, disable Secure email change in your project's email auth provider settings.
_10try await supabase.auth.update(user: UserAttributes(email: "new@email.com"))
Retrieve identities linked to a user
- The user needs to be signed in to call
userIdentities()
.
_10let identities = try await supabase.auth.userIdentities()
Link an identity to a user
- The Enable Manual Linking option must be enabled from your project's authentication settings.
- The user needs to be signed in to call
linkIdentity()
. - If the candidate identity is already linked to the existing user or another user,
linkIdentity()
will fail.
_10try await supabase.auth.linkIdentity(provider: provider)
Unlink an identity from a user
- The Enable Manual Linking option must be enabled from your project's authentication settings.
- The user needs to be signed in to call
unlinkIdentity()
. - The user must have at least 2 identities in order to unlink an identity.
- The identity to be unlinked must belong to the user.
_10// retrieve all identites linked to a user_10let identities = try await supabase.auth.userIdentities()_10_10// find the google identity _10let googleIdentity = identities.first {_10 $0.provider == .google_10}_10_10// unlink the google identity_10try await supabase.auth.unlinkIdentity(googleIdentity)
Send a password reauthentication nonce
- This method is used together with
update(user:)
when a user's password needs to be updated. - If you require your user to reauthenticate before updating their password, you need to enable the Secure password change option in your project's email provider settings.
- A user is only require to reauthenticate before updating their password if Secure password change is enabled and the user hasn't recently signed in. A user is deemed recently signed in if the session was created in the last 24 hours.
- This method will send a nonce to the user's email. If the user doesn't have a confirmed email address, the method will send the nonce to the user's confirmed phone number instead.
_10try await supabase.auth.reauthenticate()
Resend an OTP
- Resends a signup confirmation, email change, or phone change email to the user.
- Passwordless sign-ins can be resent by calling the
signInWithOTP()
method again. - Password recovery emails can be resent by calling the
resetPasswordForEmail()
method again. - This method only resends an email or phone OTP to the user if there an initial signup, email change, or phone change request was made.
- You can specify a redirect URL when you resend an email link using the
emailRedirectTo
option.
_10try await supabase.auth.resend(_10 email: "email@example.com",_10 type: .signup,_10 emailRedirectTo: URL(string: "my-app-scheme://")_10)
Set the session data
setSession()
takes in a refresh token and uses it to get a new session.- The refresh token can only be used once to obtain a new session.
- Refresh token rotation is enabled by default on all projects to guard against replay attacks.
- You can configure the
REFRESH_TOKEN_REUSE_INTERVAL
which provides a short window in which the same refresh token can be used multiple times in the event of concurrency or offline issues.
_10 try await supabase.auth.setSession(accessToken: "access_token", refreshToken: "refresh_token")
Exchange an auth code for a session
- Used when
flowType
is set topkce
in client options.
_10try await supabase.auth.exchangeCodeForSession(authCode: "34e770dd-9ff9-416c-87fa-43b31d7ef225")
Auth MFA
This section contains methods commonly used for Multi-Factor Authentication (MFA) and are invoked behind the supabase.auth.mfa
namespace.
Currently, we only support time-based one-time password (TOTP) as the 2nd factor. We don't support recovery codes but we allow users to enroll more than 1 TOTP factor, with an upper limit of 10.
Having a 2nd TOTP factor for recovery frees the user of the burden of having to store their recovery codes somewhere. It also reduces the attack surface since multiple recovery codes are usually generated compared to just having 1 backup TOTP factor.
Enroll a factor
- Currently,
totp
is the only supportedfactorType
. The returnedid
should be used to create a challenge. - To create a challenge, see
mfa.challenge()
. - To verify a challenge, see
mfa.verify()
. - To create and verify a challenge in a single step, see
mfa.challengeAndVerify()
.
_15let response = try await supabase.auth.mfa.enroll(_15 params: MFAEnrollParams(_15 issuer: "optional issuer",_15 friendlyName: "optional friendly name"_15 )_15)_15_15// Use the id to create a challenge._15// The challenge can be verified by entering the code generated from the authenticator app._15// The code will be generated upon scanning the qrCode or entering the secret into the authenticator app._15let id = response.id_15let type = response.type_15let qrCode = response.totp?.qrCode_15let secret = response.totp?.secret_15let uri = response.totp?.uri
Create a challenge
- An enrolled factor is required before creating a challenge.
- To verify a challenge, see
mfa.verify()
.
_10let response = try await supabase.auth.mfa.challenge(_10 params: MFAChallengeParams(_10 factorId: "34e770dd-9ff9-416c-87fa-43b31d7ef225"_10 )_10)
Verify a challenge
- To verify a challenge, please create a challenge first.
_10let session = try await supabase.auth.mfa.verify(_10 params: MFAVerifyParams(_10 factorId: "34e770dd-9ff9-416c-87fa-43b31d7ef225",_10 challengeId: "4034ae6f-a8ce-4fb5-8ee5-69a5863a7c15",_10 code: "123456"_10 )_10)
Create and verify a challenge
- An enrolled factor is required before invoking
challengeAndVerify()
. - Executes
mfa.challenge()
andmfa.verify()
in a single step.
_10let session = try await supabase.auth.mfa.challengeAndVerify(_10 params: MFAChallengeAndVerifyParams(_10 factorId: "34e770dd-9ff9-416c-87fa-43b31d7ef225",_10 code: "123456"_10 )_10)
Unenroll a factor
_10let response = try await supabase.auth.mfa.unenroll(_10 params: MFAUnenrollParams(_10 factorId: "34e770dd-9ff9-416c-87fa-43b31d7ef225"_10 )_10)
Get Authenticator Assurance Level
- Authenticator Assurance Level (AAL) is the measure of the strength of an authentication mechanism.
- In Supabase, having an AAL of
aal1
refers to having the 1st factor of authentication such as an email and password or OAuth sign-in whileaal2
refers to the 2nd factor of authentication such as a time-based, one-time-password (TOTP). - If the user has a verified factor, the
nextLevel
field will returnaal2
, else, it will returnaal1
.
_10let aal = try await supabase.auth.mfa.getAuthenticatorAssuranceLevel()_10let currentLevel = aal.currentLevel_10let nextLevel = aal.nextLevel_10let currentAuthenticationMethods = aal.currentAuthenticationMethods
Auth Admin
- Any method under the
supabase.auth.admin
namespace requires aservice_role
key. - These methods are considered admin methods and should be called on a trusted server. Never expose your
service_role
key in the browser.
_10import Supabase_10_10let supabase = SupabaseClient(_10 supabaseURL: supabaseURL,_10 supabaseKey: serviceRoleKey_10)_10_10// Access auth admin api_10let adminAuthClient = supabase.auth.admin
Delete a user
- The
deleteUser()
method requires the user's ID, which maps to theauth.users.id
column.
_10try await supabase.auth.admin.deleteUser(_10 id: "715ed5db-f090-4b8c-a067-640ecee36aa0"_10)
List all factors for a user
_10let factors = try await supabase.auth.mfa.listFactors()
Invokes a Supabase Edge Function.
Invoke a Supabase Edge Function.
- Requires an Authorization header.
- When you pass in a body to your function, we automatically attach the Content-Type header for
String
, andData
. If it doesn't match any of these types we assume the payload isjson
, serialize it and attach theContent-Type
header asapplication/json
. You can override this behaviour by passing in aContent-Type
header of your own.
_11struct Response: Decodable {_11 // Expected response definition_11}_11_11let response: Response = try await supabase.functions_11 .invoke(_11 "hello", _11 options: FunctionInvokeOptions(_11 body: ["foo": "bar"]_11 )_11 )
Subscribe to channel
- By default, Broadcast and Presence are enabled for all projects.
- By default, listening to database changes is disabled for new projects due to database performance and security concerns. You can turn it on by managing Realtime's replication.
- You can receive the "previous" data for updates and deletes by setting the table's
REPLICA IDENTITY
toFULL
(e.g.,ALTER TABLE your_table REPLICA IDENTITY FULL;
). - Row level security is not applied to delete statements. When RLS is enabled and replica identity is set to full, only the primary key is sent to clients.
- Use AsyncStream or callbacks for listening to changes.
_19let channel = supabase.channel("room1")_19_19let broadcastStream = channel.broadcastStream(event: "cursor-pos")_19_19await channel.subscribe()_19_19Task {_19 for await message in broadcastStream {_19 print("Cursor position received", message)_19 }_19}_19_19await channel.broadcast(_19 event: "cursor-pos",_19 message: [_19 "x": .double(.random(in: 0...1)),_19 "y": .double(.random(in: 0...1))_19 ]_19)
Unsubscribe from a channel
Unsubscribes and removes Realtime channel from Realtime client.
- Removing a channel is a great way to maintain the performance of your project's Realtime service as well as your database if you're listening to Postgres changes.
- Supabase will automatically handle cleanup 30 seconds after a client is disconnected, but unused channels may cause degradation as more clients are simultaneously subscribed.
- If you removed all channels, the client automatically disconnects from the Realtime websocket. This can be disabled in the Realtime config by setting
disconnectOnNoSubscriptions
to false.
_10let channel = supabase.channel("channelId") _10_10//..._10await supabase.removeChannel(channel)
Unsubscribe from all channels
Unsubscribes and removes all Realtime channels from Realtime client.
- Removing channels is a great way to maintain the performance of your project's Realtime service as well as your database if you're listening to Postgres changes. Supabase will automatically handle cleanup 30 seconds after a client is disconnected, but unused channels may cause degradation as more clients are simultaneously subscribed.
- If you removed all channels, the client automatically disconnects from the Realtime websocket. This can be disabled in the Realtime config by setting
disconnectOnNoSubscriptions
to false.
_10await supabase.removeAllChannels()
Retrieve all channels
Returns all Realtime channels.
_10let channels = supabase.channels
Create a bucket
- RLS policy permissions required:
buckets
table permissions:insert
objects
table permissions: none
- Refer to the Storage guide on how access control works
_10try await supabase.storage_10 .createBucket(_10 "avatars", _10 options: BucketOptions(_10 public: false,_10 allowedMimeTypes: ["image/png"],_10 fileSizeLimit: 1024_10 )_10 )
Retrieve a bucket
- RLS policy permissions required:
buckets
table permissions:select
objects
table permissions: none
- Refer to the Storage guide on how access control works
_10let bucket = try await supabase.storage_10 .getBucket("avatars")
List all buckets
- RLS policy permissions required:
buckets
table permissions:select
objects
table permissions: none
- Refer to the Storage guide on how access control works
_10try await supabase.storage_10 .listBuckets()
Update a bucket
- RLS policy permissions required:
buckets
table permissions:select
andupdate
objects
table permissions: none
- Refer to the Storage guide on how access control works
_10try await supabase.storage_10 .updateBucket(_10 "avatars", _10 options: BucketOptions(_10 public: false,_10 fileSizeLimit: 1024,_10 allowedMimeTypes: ["image/png"]_10 )_10 )
Delete a bucket
- RLS policy permissions required:
buckets
table permissions:select
anddelete
objects
table permissions: none
- Refer to the Storage guide on how access control works
_10try await supabase.storage_10 .deleteBucket("avatars")
Empty a bucket
- RLS policy permissions required:
buckets
table permissions:select
objects
table permissions:select
anddelete
- Refer to the Storage guide on how access control works
_10try await supabase.storage_10 .emptyBucket("avatars")
Upload a file
- RLS policy permissions required:
buckets
table permissions: noneobjects
table permissions: onlyinsert
when you are uploading new files andselect
,insert
andupdate
when you are upserting files
- Refer to the Storage guide on how access control works
_13let fileName = "avatar1.png"_13_13try await supabase.storage_13 .from("avatars")_13 .upload(_13 path: "public/\(fileName)",_13 file: fileData,_13 options: FileOptions(_13 cacheControl: "3600",_13 contentType: "image/png",_13 upsert: false_13 )_13 )
Download a file
- RLS policy permissions required:
buckets
table permissions: noneobjects
table permissions:select
- Refer to the Storage guide on how access control works
_10let data = try await supabase.storage_10 .from("avatars")_10 .download(path: "folder/avatar1.png")
List all files in a bucket
- RLS policy permissions required:
buckets
table permissions: noneobjects
table permissions:select
- Refer to the Storage guide on how access control works
_10let files = try await supabase.storage_10 .from("avatars")_10 .list(_10 path: "folder",_10 options: SearchOptions(_10 limit: 100,_10 offset: 0,_10 sortBy: SortBy(column: "name", order: "asc")_10 )_10 )
Replace an existing file
- RLS policy permissions required:
buckets
table permissions: noneobjects
table permissions:update
andselect
- Refer to the Storage guide on how access control works
_13let fileName = "avatar1.png"_13_13try await supabase.storage_13 .from("avatars")_13 .update(_13 path: "public/\(fileName)",_13 file: fileData,_13 options: FileOptions(_13 cacheControl: "3600",_13 contentType: "image/png",_13 upsert: true_13 )_13 )
Move an existing file
- RLS policy permissions required:
buckets
table permissions: noneobjects
table permissions:update
andselect
- Refer to the Storage guide on how access control works
_10try await supabase_10 .storage_10 .from("avatars")_10 .move(from: "public/avatar1.png", to: "private/avatar2.png")
Copy an existing file
- RLS policy permissions required:
buckets
table permissions: noneobjects
table permissions:insert
andselect
- Refer to the Storage guide on how access control works
_10try await supabase_10 .storage_10 .from("avatars")_10 .copy(from: "public/avatar1.png", to: "private/avatar2.png")
Delete files in a bucket
- RLS policy permissions required:
buckets
table permissions: noneobjects
table permissions:delete
andselect
- Refer to the Storage guide on how access control works
_10try await supabase.storage_10 .from("avatars")_10 .remove(paths: ["folder/avatar1.png"])
Create a signed URL
- RLS policy permissions required:
buckets
table permissions: noneobjects
table permissions:select
- Refer to the Storage guide on how access control works
_10let signedURL = try await supabase.storage_10 .from("avatars")_10 .createSignedURL(path: "folder/avatar1.png", expiresIn: 60)
Create signed URLs
- RLS policy permissions required:
buckets
table permissions: noneobjects
table permissions:select
- Refer to the Storage guide on how access control works
_10let urls = try await supabase_10 .storage_10 .from("avatars")_10 .createSignedURLs(paths: ["folder/avatar1.png", "folder/avatar2.png"], expiresIn: 60)
Retrieve public URL
- The bucket needs to be set to public, either via updateBucket() or by going to Storage on supabase.com/dashboard, clicking the overflow menu on a bucket and choosing "Make public"
- RLS policy permissions required:
buckets
table permissions: noneobjects
table permissions: none
- Refer to the Storage guide on how access control works
_10let publicURL = try supabase.storage_10 .from("public-bucket")_10 .getPublicURL(path: "folder/avatar1.png")