ClientBuilder provides a type-safe, fluent API for configuring and creating XMTP clients.
Structure
Creating a Builder
From Client
Strategy for initializing or loading the client’s identity. Options:
IdentityStrategy::new(inbox_id, account_identifier, nonce, legacy_identity)- Create new or restore existingIdentityStrategy::CachedOnly- Load from local database only
Direct Construction
Configuration Methods
Required Configuration
api_clients(api_client, sync_api_client)
Set both the main API client and sync API client.
Client for network operations (publishing, querying)
Client for background sync operations
ClientBuilder<A, S, Db> with new API client type
store(db)
Set the encrypted message store.
Database implementation (typically
EncryptedMessageStore<NativeDb>)ClientBuilder<ApiClient, S, NewDb>
default_mls_store()
Use the default SQLite-based MLS key-value store. Must be called after store().
Returns: Result<ClientBuilder<ApiClient, SqlKeyStore, Db>, ClientBuilderError>
mls_storage(storage)
Provide a custom MLS storage implementation.
Custom MLS storage provider
ClientBuilder<ApiClient, NewS, Db>
Smart Contract Verifier
One of these methods is required:with_scw_verifier(verifier)
Provide a custom smart contract signature verifier.
Custom verifier implementation
with_remote_verifier()
Use the default API-based verifier. Requires api_client to be set first.
Returns: Result<ClientBuilder<ApiClient, S, Db>, ClientBuilderError>
Optional Configuration
identity(identity)
Provide a pre-initialized identity instead of using the identity strategy.
Pre-configured identity object
device_sync_worker_mode(mode)
Configure the device sync worker behavior.
DeviceSyncMode::Enabled- Sync preferences across devices (default)DeviceSyncMode::Disabled- No cross-device sync
with_device_sync_worker_mode(mode)
Same as above but accepts Option<DeviceSyncMode>.
fork_recovery_opts(opts)
Configure fork recovery behavior for handling group state inconsistencies.
Fork recovery configuration:
enable_recovery_requests:ForkRecoveryPolicy- When to request recoverygroups_to_request_recovery:Vec<String>- Specific groups to recoverdisable_recovery_responses:bool- Don’t respond to recovery requestsworker_interval_ns:Option<u64>- Custom worker polling interval
version(version_info)
Set client version information.
Version metadata for this client
maybe_version(version)
Set version information if provided.
Optional version metadata
with_allow_offline(allow_offline)
Skip network calls during client construction.
If true, don’t fetch identity updates from network during build
with_disable_workers(disable_workers)
Disable background workers (sync, key rotation, etc.).
If true, no background workers will be started
cursor_store(cursor_store)
Provide a custom cursor store for sync operations.
Custom cursor store implementation
Debugging and Monitoring
enable_api_debug_wrapper()
Wrap the API client to print statistics on errors. Requires api_client to be set.
Returns: Result<ClientBuilder<ApiDebugWrapper<ApiClient>, S, Db>, ClientBuilderError>
enable_api_stats()
Wrap the API client to track statistics. Requires api_client to be set.
Returns: Result<ClientBuilder<TrackedStatsClient<ApiClient>, S, Db>, ClientBuilderError>
Building the Client
build()
Asynchronously build the client, potentially making network calls.
Returns: Result<Client<ContextParts<ApiClient, S, Db>>, ClientBuilderError>
- Validates all required parameters are set
- Wraps API clients with retry logic
- Initializes or loads identity based on strategy
- Loads identity updates from network (unless
allow_offlineis true) - Creates shared context with all dependencies
- Registers and spawns background workers (unless disabled)
- Performs cleanup of old data
build_offline()
Build the client synchronously without network access.
Returns: Result<Client<ContextParts<ApiClient, S, Db>>, ClientBuilderError>
Fails with ClientBuilderError::OfflineBuildFailed if network access would be required.
Type Parameters
The builder uses three generic type parameters:Type implementing
XmtpApi + XmtpQuery + 'staticTypically XmtpApiClient or wrapped variants like ApiDebugWrapper<XmtpApiClient>MLS storage provider type implementing
XmtpMlsStorageProvider + 'staticTypically SqlKeyStore<DbQuery>Database type implementing
XmtpDb + 'staticDefaults to xmtp_db::DefaultStore. Typically EncryptedMessageStore<NativeDb>Error Types
Complete Example
See Also
- Client Overview
- Client Methods
- Source:
crates/xmtp_mls/src/builder.rs:70
