connect() on it. The client SDK then downloads data from the PowerSync Service into the client-side SQLite database, and uploads local mutations to your backend. To do both, the SDK uses two parts that you implement:
When you’re getting started, we recommend that you focus on the read path first: implement the authenticator, pass it to
connect() with your instance URL, and check that synced data reaches your app. Then add the uploader to connect().
Add the uploader before your app writes to the local database. Until then, mutations wait in the upload queue, and synced data stops updating: the SDK can’t apply new data while they wait, and it reports a download error on the sync status.
You can also connect with only one of the two parts. See Connect for Downloads or Uploads Only.
The authenticator and mutation uploader require
@powersync/web 2.6.0, @powersync/react-native 2.4.0, @powersync/node 1.3.0, @powersync/capacitor 0.10.0, Dart/Flutter 2.6.0, Kotlin 1.16.0, Swift 1.17.0, PowerSync.Common 1.0.1, Rust 0.2.0 or tauri-plugin-powersync 0.3.0, or later.Authenticator
The SDK callsresolveCredentials() on your authenticator to get a JWT for the PowerSync Service. How you get the JWT depends on your authentication setup:
- Development: Return a development token.
- JWTs from your authentication provider: Some providers issue JWTs that PowerSync can verify directly. Return the JWT from your app’s authentication session. See Common Authentication Providers.
- Custom authentication: Request a JWT from your backend. See Custom Authentication.
When resolveCredentials() is Called
When the SDK needs a new token, it calls invalidateCredentials() and then resolveCredentials(). The SDK needs a new token when the current token has 30 seconds or less remaining, when the token has expired (for example while the device was offline), or when the PowerSync Service rejects the token with a 401 status.
The SDK can call resolveCredentials() on every connection or request, so make it cheap. If your authenticator requests its JWT from your backend, cache the JWT and clear the cache in invalidateCredentials(). If it reads the token from an auth library that stores and refreshes it, such as Supabase, return that token, and ask the library to refresh the session in invalidateCredentials(). Either way, after invalidateCredentials(), the next resolveCredentials() call must return a new token. For an example in each SDK, see Cache the Token in Your Authenticator.
Recommended JWT Expiry Duration
A JWT expiry (TTL) of 5 to 60 minutes works well with PowerSync:- Shorter JWTs (5–15 minutes): More secure with minimal overhead, since the SDK only needs a new token when the current one is near expiry.
- Longer JWTs (30–60 minutes): Fewer token requests and a simpler backend implementation.
resolveCredentials() can return a valid token when called.
Avoid JWTs with a TTL shorter than 30 seconds. The SDK gets a new token when 30 seconds or less remain, so a shorter TTL skips that step and always goes through the expired-token reconnection.
The JWT only authenticates the client SDK’s connection to the PowerSync Service. When it expires, your app’s own sign-in session (for example with Supabase Auth or Firebase Auth) is not affected. That session has its own lifecycle and refresh mechanism.
Mutation Uploader
The client SDK records every mutation in an upload queue and calls your mutation uploader,uploadData(), to upload the queued mutations to your write endpoint. If uploadData() throws, the SDK retries it. To pick how to upload mutations, see Choosing Your Setup.
When uploadData() is Called
The client SDK calls uploadData() automatically. You never call it directly. The SDK calls it in these cases:
- After your app writes - Any
INSERT,UPDATE, orDELETEon a PowerSync table adds an entry to the internal upload queue (theps_crudtable). The SDK monitors this table for changes and triggersuploadData()shortly after. See How Local Writes Are Detected below for details on the underlying mechanism. - On initial connection / reconnection - When
connect()establishes (or re-establishes) a sync stream and the first message is received from the PowerSync Service, the SDK triggersuploadData()to flush any writes that were made while offline or disconnected. - After an error, with retry - If
uploadData()throws an error, the SDK waits for a configurable (default: 5 seconds) and then retries. This continues until the upload succeeds or the sync stream is disconnected.
How Local Writes Are Detected
By default, each PowerSync-managed table is exposed in SQLite as a view withINSTEAD OF INSERT/UPDATE/DELETE triggers (generated by the PowerSync SQLite core extension when your client-side schema is applied). When you write to a PowerSync table, those triggers do two things atomically:
- Apply the change to the underlying
ps_data__<table>table. - Append an entry to
ps_cruddescribing the mutation (see Write Operations for the entry format).
powersync_create_raw_table_crud_trigger helper.)
The SDK then detects new ps_crud entries by subscribing to SQLite’s table-update notifications, filtered for changes to ps_crud. This is the same mechanism that powers reactive watch queries. When a change is observed, the SDK schedules an uploadData() call subject to the configured throttle interval.
The mechanism is consistent across all PowerSync SDKs (JavaScript, Dart, Kotlin, Swift, .NET, Rust), since the triggers and ps_crud table are defined by the shared PowerSync SQLite core extension.
Upload Loop Behavior
The SDK callsuploadData() in a loop - not just once per trigger. After each successful uploadData() call, the SDK checks whether there are more items in the upload queue. If there are, it calls uploadData() again immediately. The loop continues until the queue is empty. This means your uploadData() implementation only needs to process one batch (or one transaction) per call.
Once the queue is empty, the SDK updates an internal write checkpoint used for consistency tracking.
Throttling
To avoid excessive calls, upload triggers are throttled. Mutations in quick succession (e.g., multipleINSERT statements) are coalesced so that uploadData() is invoked at most once per throttle interval. If an upload is already in progress when a new write occurs, the SDK will trigger another upload after the current one completes.
The default throttle interval varies by SDK:
You can configure the throttle interval in the options when you call
connect():
Error Handling
WhenuploadData() throws an error:
- The SDK logs the error and updates the sync status with an
uploadError. - It waits for the retry delay (default: 5 seconds).
- It retries the upload from the beginning of the queue.
- If the sync stream disconnects during the retry wait, the upload loop exits and will resume when the connection is re-established.
Stalled Upload Queue Detection
If the SDK detects that the same CRUD entry is at the front of the queue across consecutive upload iterations (i.e.,uploadData() returned without error but didn’t call .complete() on the batch or transaction), it logs a warning:
“Potentially previously uploaded CRUD entries are still present in the upload queue. Make sure to handle uploads and complete CRUD transactions or batches by calling and awaiting their [.complete()] method. The next upload iteration will be delayed.”After logging this warning, the SDK throws an internal error, which causes it to enter the retry delay path (default: 5 seconds) before attempting the upload again. This prevents a tight loop when
uploadData() consistently fails to process entries.
This typically means your uploadData() implementation is not calling .complete() after successfully processing entries. Always call .complete() on the CrudBatch or CrudTransaction to remove processed entries from the queue.
Backend Connector
A backend connector is the previous way to connect: a single object that you pass toconnect(). Its fetchCredentials() method returns a JWT and the instance URL, and its uploadData() method uploads mutations. Backend connector APIs remain supported in every SDK except Rust and Tauri, which removed them in Rust SDK 0.2.0 and tauri-plugin-powersync 0.3.0. We plan to deprecate backend connectors in a future release, so use an authenticator and a mutation uploader in new code. This section helps you migrate an existing connector. If you upgrade a .NET app to v1, follow the connector upgrade instructions.
To migrate a backend connector:
- Move the token logic from
fetchCredentials()intoresolveCredentials(), which returns only the token. Pass the instance URL toconnect()instead. - Pass your
uploadData()toconnect()as the mutation uploader. Its parameters can . - The SDK caches what
fetchCredentials()returns, but not whatresolveCredentials()returns. If your connector requested the token from your backend, cache it in your authenticator. See WhenresolveCredentials()is Called. - If your backend connector handles checkpoint requests, move
postCheckpointRequest()to your authenticator. See Authenticator Changes.
When fetchCredentials() is Called
For a backend connector, the SDK caches what fetchCredentials() returns. It calls fetchCredentials() in the same cases as resolveCredentials(): on the first connection, when the token is about to expire, after it has expired, and after a 401 response. Return a new token whenever fetchCredentials() is called, even if the previous one hasn’t expired yet.
Example Implementation
The reference write implementation includes a complete mutation uploader in TypeScript, for the JavaScript Web and React Native SDKs, and the write endpoint that it uploads to. For a starter authenticator and mutation uploader in each SDK’s language, see Client Connection Examples. For a minimaluploadData() in each SDK, see Usage Examples.