Skip to main content
The ClientBuilder provides a type-safe, fluent API for configuring and creating XMTP clients.

Structure

Creating a Builder

From Client

IdentityStrategy
required
Strategy for initializing or loading the client’s identity. Options:
  • IdentityStrategy::new(inbox_id, account_identifier, nonce, legacy_identity) - Create new or restore existing
  • IdentityStrategy::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.
impl XmtpApi + XmtpQuery + 'static
required
Client for network operations (publishing, querying)
impl XmtpApi + XmtpQuery + 'static
required
Client for background sync operations
Returns: ClientBuilder<A, S, Db> with new API client type

store(db)

Set the encrypted message store.
impl XmtpDb
required
Database implementation (typically EncryptedMessageStore<NativeDb>)
Returns: 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.
impl XmtpMlsStorageProvider + 'static
required
Custom MLS storage provider
Returns: ClientBuilder<ApiClient, NewS, Db>

Smart Contract Verifier

One of these methods is required:
with_scw_verifier(verifier)
Provide a custom smart contract signature verifier.
impl SmartContractSignatureVerifier + 'static
required
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.
Identity
Pre-configured identity object

device_sync_worker_mode(mode)

Configure the device sync worker behavior.
DeviceSyncMode
  • 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.
ForkRecoveryOpts
Fork recovery configuration:
  • enable_recovery_requests: ForkRecoveryPolicy - When to request recovery
  • groups_to_request_recovery: Vec<String> - Specific groups to recover
  • disable_recovery_responses: bool - Don’t respond to recovery requests
  • worker_interval_ns: Option<u64> - Custom worker polling interval

version(version_info)

Set client version information.
VersionInfo
Version metadata for this client

maybe_version(version)

Set version information if provided.
Option<VersionInfo>
Optional version metadata

with_allow_offline(allow_offline)

Skip network calls during client construction.
Option<bool>
If true, don’t fetch identity updates from network during build

with_disable_workers(disable_workers)

Disable background workers (sync, key rotation, etc.).
bool
If true, no background workers will be started

cursor_store(cursor_store)

Provide a custom cursor store for sync operations.
Arc<dyn CursorStore>
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>
Process:
  1. Validates all required parameters are set
  2. Wraps API clients with retry logic
  3. Initializes or loads identity based on strategy
  4. Loads identity updates from network (unless allow_offline is true)
  5. Creates shared context with all dependencies
  6. Registers and spawns background workers (unless disabled)
  7. 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
Type implementing XmtpApi + XmtpQuery + 'staticTypically XmtpApiClient or wrapped variants like ApiDebugWrapper<XmtpApiClient>
type
MLS storage provider type implementing XmtpMlsStorageProvider + 'staticTypically SqlKeyStore<DbQuery>
type
Database type implementing XmtpDb + 'staticDefaults to xmtp_db::DefaultStore. Typically EncryptedMessageStore<NativeDb>

Error Types

Complete Example

See Also