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.
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.
1let client = SupabaseClient(supabaseURL: URL(string: "https://xyzcompany.supabase.co")!, supabaseKey: "publishable-or-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
apikeyis 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
Codableto easily decode your database responses.
1struct Instrument: Decodable {2 let id: Int3 let name: String4}56let instruments: [Instrument] = try await supabase.database7 .from("instruments")8 .select()9 .execute()10 .valueInsert data
1struct Instrument: Encodable {2 let id: Int3 let name: String4}56let instrument = Instrument(id: 1, name: "piano")78try await supabase.database9 .from("instruments")10 .insert(instrument)11 .execute()Update data
update()should always be combined with Filters to target the item(s) you wish to update.
1try await supabase.database2 .from("instruments")3 .update(["name": "piano"])4 .eq("id", value: 1)5 .execute()Upsert data
- Primary keys must be included in
valuesto use upsert.
1struct Instrument: Encodable {2 let id: Int3 let name: String4}5try await supabase.database6 .from("instruments")7 .upsert(Instrument(id: 1, name: "piano"))8 .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 throughSELECTpolicies are deleted. Note that by default no rows are visible, so you need at least oneSELECT/ALLpolicy that makes the rows visible.
1try await supabase.database2 .from("instruments")3 .delete()4 .eq("id", value: 1)5 .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.
1create or replace function hello_world() returns text as $$2 select 'Hello world';3$$ language sql;1let value: String = try await supabase.database2 .rpc("hello_world")3 .execute()4 .valueUsing 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.
1try await supabase.database2 .from("cities")3 .select("name, country_id")4 .eq("name", value: "The Shire") // Correct56try await supabase.database7 .from("cities")8 .eq("name", value: "The Shire") // Incorrect9 .select("name, country_id")Match an associated value
1try await supabase.database2 .from("instruments")3 .select("name")4 .match(["id": 2, "name": "viola"])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.1.not("name", operator: .eq, value: "violin")2.not("arraycol", operator: .cs, value: #"{"a","b"}"#) // Use Postgres array {} for array column and 'cs' for contains.3.not("rangecol", operator: .cs, value: "(1,2]") // Use Postgres range syntax for range column.4.not("id", operator: .in, value: "(6,7)") // Use Postgres list () and 'in' for in_ filter.5.not("id", operator: .in, value: "(\(mylist.join(separator: ",")))") // You can insert a Swift list array.
1try await supabase.database2 .from("instruments")3 .select()4 .not("name", operator: .is, value: "")5 .execute()Match at least one filter
or() expects you to use the raw PostgREST syntax for the filter names and values.
1.or(#"id.in.(5,6,7), arraycol.cs.{"a","b"}"#) // Use `()` for `in` filter, `{}` for array values and `cs` for `contains()`.2.or(#"id.in.(5,6,7), arraycol.cd.{"a","b"}"#) // Use `cd` for `containedBy()`1try await supabase.database2 .from("instruments")3 .select("name")4 .or("id.eq.2,name.eq.cello")Match the filter
filter() expects you to use the raw PostgREST syntax for the filter values.
1.filter("id", operator: .in, value: "(5,6,7)") // Use `()` for `in` filter2.filter("arraycol", operator: .cs, value: #"{"a","b"}"#) // Use `cs` for `contains()`, `{}` for array values1try await supabase.database2 .from("instruments")3 .select()4 .filter("name", operator: .in, value: #"("cello","guzheng")"#)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.
1try await database.database2 .from("instruments")3 .upsert(InstrumentModel(id: 1, name: "piano"))4 .select()5 .execute()Order the results
Order the query result by column.
1try await supabase.database2 .from("instruments")3 .select("id, name")4 .order("id", ascending: false)5 .execute()Limit the number of rows returned
Limit the query result by count.
1try await supabase.database2 .from("instruments")3 .select("id, name")4 .limit(1)5 .execute()Limit the query to a range
Limit the query result by from and to inclusively.
1try await supabase.database2 .from("orchestral_sections")3 .select(4 """5 name,6 instruments (7 name8 )9 """10 )11 .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.
1try await supabase.database2 .from("instruments")3 .select("name")4 .limit(1)5 .single()6 .execute()Retrieve as a CSV
1try await supabase2 .from("instruments")3 .select()4 .csv()5 .execute()Overview
- The auth methods can be accessed via the
supabase.authnamespace.
1let supabase = SupabaseClient(supabaseURL: URL(string: "https://xyzcompany.supabase.co")!, supabaseKey: "publishable-or-anon-key")2let auth = supabase.authCreate 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
useris returned butsessionis null. - If Confirm email is disabled, both a
userand asessionare returned.
- If Confirm email is enabled, a
- When the user confirms their email address, they are redirected to the
SITE_URLby default. You can modify yourSITE_URLor add additional redirect URLs in your project. - If signUp() is called for an existing confirmed user:
- If Confirm email is enabled in your project, an obfuscated/fake user object is returned.
- If Confirm email is disabled, the error message,
User already registeredis returned.
- To fetch the currently logged-in user, refer to
getUser().
1try await supabase.auth.signUp(2 email: "example@email.com",3 password: "example-password"4)Listen to auth events
- Types of auth events:
INITIAL_SESSION,SIGNED_IN,SIGNED_OUT,TOKEN_REFRESHED,USER_UPDATED,PASSWORD_RECOVERY,MFA_CHALLENGE_VERIFIED - The
INITIAL_SESSIONcan be used to allow you to invoke the callback function whenauthStateChangesis first called.
1for await (event, session) in await supabase.auth.authStateChanges {2 print(event, session)3}Sign in a user
- Requires either an email and password or a phone number and password.
1try await supabase.auth.signIn(2 email: "example@email.com",3 password: "example-password"4)Sign in with ID token (native sign-in)
1let session = try await supabase.auth.signInWithIdToken(2 credentials: OpenIDConnectCredentials(3 provider: .apple,4 idToken: "your-id-token",5 nonce: "your nonce"6 )7)Sign in a user through OTP
- 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 setshouldCreateUsertofalse. - 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 }}. - When using magic links, specify a
redirectTothat matches a configured url scheme in your iOS app, so Supabase can correctly redirect back to your app. - See our Twilio Phone Auth Guide for details about configuring WhatsApp sign in.
1try await supabase.auth.signInWithOTP(2 email: "example@email.com",3 redirectTo: URL(string: "my-app-scheme://")!4)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.
1let url = try await supabase.auth.getOAuthSignInURL(provider: .github)23let session = ASWebAuthenticationSession(url: url, callbackURLScheme: "my-app-scheme") { url, error in4 guard let url else { return }56 Task {7 try await supabase.auth.session(from: url)8 }9}1011session.presentationContextProvider = self // yours ASWebAuthenticationPresentationContextProviding implementation.1213session.start()Sign out a user
- In order to use the
signOut()method, the user needs to be signed in first.
1try await supabase.auth.signOut()Verify and log in through OTP
- The
verifyOTPmethod takes in different verification types. If a phone number is used, the type can either besmsorphone_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
verifyOTPto sign up / sign-in a user.
1try await supabase.auth.verifyOTP(2 phone: "+13334445555",3 token: "123456",4 type: .sms5)Retrieve a session
- Returns the session, refreshing it if necessary. If no session can be found, a
GoTrueError.sessionNotFounderror is thrown.
1try await supabase.auth.sessionRetrieve a new session
- This method will refresh the session whether the current one is expired or not.
1let 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.useris recommended.
1let 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.
1try await supabase.auth.update(user: UserAttributes(email: "new@email.com"))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_INTERVALwhich provides a short window in which the same refresh token can be used multiple times in the event of concurrency or offline issues.
1try await supabase.auth.setSession(accessToken: "access_token", refreshToken: "refresh_token")Exchange an auth code for a session
- Used when
flowTypeis set topkcein client options.
1try 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,
totpis the only supportedfactorType. The returnedidshould 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().
1let response = try await supabase.auth.mfa.enroll(2 params: MFAEnrollParams(3 issuer: "optional issuer",4 friendlyName: "optional friendly name"5 )6)78// Use the id to create a challenge.9// The challenge can be verified by entering the code generated from the authenticator app.10// The code will be generated upon scanning the qrCode or entering the secret into the authenticator app.11let id = response.id12let type = response.type13let qrCode = response.totp?.qrCode14let secret = response.totp?.secret15let uri = response.totp?.uriCreate a challenge
- An enrolled factor is required before creating a challenge.
- To verify a challenge, see
mfa.verify().
1let response = try await supabase.auth.mfa.challenge(2 params: MFAChallengeParams(3 factorId: "34e770dd-9ff9-416c-87fa-43b31d7ef225"4 )5)Verify a challenge
- To verify a challenge, please create a challenge first.
1let session = try await supabase.auth.mfa.verify(2 params: MFAVerifyParams(3 factorId: "34e770dd-9ff9-416c-87fa-43b31d7ef225",4 challengeId: "4034ae6f-a8ce-4fb5-8ee5-69a5863a7c15",5 code: "123456"6 )7)Create and verify a challenge
- An enrolled factor is required before invoking
challengeAndVerify(). - Executes
mfa.challenge()andmfa.verify()in a single step.
1let session = try await supabase.auth.mfa.challengeAndVerify(2 params: MFAChallengeAndVerifyParams(3 factorId: "34e770dd-9ff9-416c-87fa-43b31d7ef225",4 code: "123456"5 )6)Unenroll a factor
1let response = try await supabase.auth.mfa.unenroll(2 params: MFAUnenrollParams(3 factorId: "34e770dd-9ff9-416c-87fa-43b31d7ef225"4 )5)Get Authenticator Assurance Level
- Authenticator Assurance Level (AAL) is the measure of the strength of an authentication mechanism.
- In Supabase, having an AAL of
aal1refers to having the 1st factor of authentication such as an email and password or OAuth sign-in whileaal2refers to the 2nd factor of authentication such as a time-based, one-time-password (TOTP). - If the user has a verified factor, the
nextLevelfield will returnaal2, else, it will returnaal1.
1let aal = try await supabase.auth.mfa.getAuthenticatorAssuranceLevel()2let currentLevel = aal.currentLevel3let nextLevel = aal.nextLevel4let currentAuthenticationMethods = aal.currentAuthenticationMethodsInvokes 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-Typeheader asapplication/json. You can override this behaviour by passing in aContent-Typeheader of your own.
1struct Response: Decodable {2 // Expected response definition3}45let response: Response = try await supabase.functions6 .invoke(7 "hello",8 options: FunctionInvokeOptions(9 body: ["foo": "bar"]10 )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 IDENTITYtoFULL(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.
1let channel = supabase2 .realtime3 .channel("room1")45channel6 .on("broadcast", filter: ChannelFilter(event: "cursor-pos")) { message in7 print("Cursor position received!", message.payload)8 }9 .subscribe { status, error in10 if status == .subscribed {11 Task {12 await channel.send(13 type: .broadcast,14 event: "cursor-pos",15 payload: ["x": Double.random(in: 0...1), "y": Double.random(in: 0...1)]16 )17 }18 }19 }Unsubscribe from a channel
- 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.
1supabase.realtime.remove(myChannel)Unsubscribe from all channels
- 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.
1supabase.realtime.removeAllChannels()Retrieve all channels
1let channels = supabase.realtime.channelsBroadcast a message
Broadcast a message to all connected clients to a channel.
- When using REST you don't need to subscribe to the channel
1supabase.realtime2 .channel("room1")3 .subscribe { status, error in4 if status == .subscribed {5 Task {6 await channel.send(7 type: "broadcast",8 event: "cursor-pos",9 payload: ["x": Double.random(in: 0...1), "y": Double.random(in: 0...1)]10 )11 }12 }13 }File Buckets
This section contains methods for working with File Buckets.
List all buckets
- RLS policy permissions required:
bucketstable permissions:selectobjectstable permissions: none
- Refer to the Storage guide on how access control works
1try await supabase.storage2 .listBuckets()Retrieve a bucket
- RLS policy permissions required:
bucketstable permissions:selectobjectstable permissions: none
- Refer to the Storage guide on how access control works
1let bucket = try await supabase.storage2 .getBucket("avatars")Create a bucket
- RLS policy permissions required:
bucketstable permissions:insertobjectstable permissions: none
- Refer to the Storage guide on how access control works
1try await supabase.storage2 .createBucket(3 "avatars",4 options: BucketOptions(5 public: false,6 allowedMimeTypes: ["image/png"],7 fileSizeLimit: 10248 )9 )Empty a bucket
- RLS policy permissions required:
bucketstable permissions:selectobjectstable permissions:selectanddelete
- Refer to the Storage guide on how access control works
1try await supabase.storage2 .emptyBucket("avatars")Update a bucket
- RLS policy permissions required:
bucketstable permissions:selectandupdateobjectstable permissions: none
- Refer to the Storage guide on how access control works
1try await supabase.storage2 .updateBucket(3 "avatars",4 options: BucketOptions(5 public: false,6 fileSizeLimit: 1024,7 allowedMimeTypes: ["image/png"]8 )9 )Delete a bucket
- RLS policy permissions required:
bucketstable permissions:selectanddeleteobjectstable permissions: none
- Refer to the Storage guide on how access control works
1try await supabase.storage2 .deleteBucket("avatars")Upload a file
- RLS policy permissions required:
bucketstable permissions: noneobjectstable permissions: onlyinsertwhen you are uploading new files andselect,insertandupdatewhen you are upserting files
- Refer to the Storage guide on how access control works
1let fileName = "avatar1.png"23try await supabase.storage4 .from("avatars")5 .upload(6 path: "public/\(fileName)",7 file: fileData,8 options: FileOptions(9 cacheControl: "3600",10 contentType: "image/png",11 upsert: false12 )13 )Replace an existing file
- RLS policy permissions required:
bucketstable permissions: noneobjectstable permissions:updateandselect
- Refer to the Storage guide on how access control works
1let fileName = "avatar1.png"23try await supabase.storage4 .from("avatars")5 .update(6 path: "public/\(fileName)",7 file: fileData,8 options: FileOptions(9 cacheControl: "3600",10 contentType: "image/png",11 upsert: true12 )13 )Move an existing file
- RLS policy permissions required:
bucketstable permissions: noneobjectstable permissions:updateandselect
- Refer to the Storage guide on how access control works
1try await supabase2 .storage3 .from("avatars")4 .move(from: "public/avatar1.png", to: "private/avatar2.png")Copy an existing file
- RLS policy permissions required:
bucketstable permissions: noneobjectstable permissions:insertandselect
- Refer to the Storage guide on how access control works
1try await supabase2 .storage3 .from("avatars")4 .copy(from: "public/avatar1.png", to: "private/avatar2.png")Create a signed URL
- RLS policy permissions required:
bucketstable permissions: noneobjectstable permissions:select
- Refer to the Storage guide on how access control works
1let signedURL = try await supabase.storage2 .from("avatars")3 .createSignedURL(path: "folder/avatar1.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:
bucketstable permissions: noneobjectstable permissions: none
- Refer to the Storage guide on how access control works
1let publicURL = try supabase.storage2 .from("public-bucket")3 .getPublicURL(path: "folder/avatar1.png")Download a file
- RLS policy permissions required:
bucketstable permissions: noneobjectstable permissions:select
- Refer to the Storage guide on how access control works
1let data = try await supabase.storage2 .from("avatars")3 .download(path: "folder/avatar1.png")Delete files in a bucket
- RLS policy permissions required:
bucketstable permissions: noneobjectstable permissions:deleteandselect
- Refer to the Storage guide on how access control works
1try await supabase.storage2 .from("avatars")3 .remove(paths: ["folder/avatar1.png"])List all files in a bucket
- RLS policy permissions required:
bucketstable permissions: noneobjectstable permissions:select
- Refer to the Storage guide on how access control works
1let files = try await supabase.storage2 .from("avatars")3 .list(4 path: "folder",5 options: SearchOptions(6 limit: 100,7 offset: 0,8 sortBy: SortBy(column: "name", order: "asc")9 )10 )