Client configuration and request lifecycle
The React Native SDK connects application code to Appwrite through one shared Client context and a set of service APIs. The client keeps project, platform, locale, authentication, environment, and optional live-delivery settings together. Each service describes an operation, while the client turns that description into a request and returns a common result or error.
System boundary
The SDK is the application-side boundary for Appwrite API calls. It keeps request context consistent while service methods describe the operation that the application wants to perform.
Client context
The Client in src/client.ts is the shared context for HTTP calls and realtime connections. It holds the API endpoint, platform, project environment, locale, authentication values, and optional push broker settings. Services keep a reference to this client, so later calls use the client’s current context.
Client configuration is mutable. An application can change the active session, environment, or locale on the shared client, and later calls use the new values. An optional push endpoint and stable push client ID provide the connection details needed by the push service. The client also identifies the SDK and the response format expected by Appwrite. Setup order and platform registration are covered in Client setup and platform integration.
References: src/client.ts• L281-304 src/client.ts• L328-573 src/client.ts• L897-927 src/client.ts• L930-943 src/…/push.ts• L204-246
Service boundary
Each service groups related Appwrite capabilities. A service method supplies the operation path, required inputs, payload, and expected response mode. The shared client then applies the common request rules.
This separation gives application code one simple flow: configure one client, create services with that client, and let each service use the shared request context.
| Responsibility | Owning boundary |
|---|---|
| Endpoint, project, environment, platform, locale, authentication, and live-delivery context | Client |
| Operation path, required inputs, and operation payload | Service method |
| Headers, body or query encoding, response conversion, and common errors | Shared client call |
References: src/service.ts• L4-10 src/client.ts• L930-997
Request lifecycle
An application request moves from a service operation to a shared client call. The service supplies operation-specific data, while the client supplies the wire contract used to reach Appwrite.
This diagram shows how an application call becomes a delivered request and then returns as a value or shared error.
flowchart LR
A[Application] --> B[Service method]
B --> C[Path and payload]
C --> D[Shared client context]
D --> E[Query or body serialization]
E --> F[Fetch to Appwrite]
F --> G[Parsed result]
F --> H[AppwriteException]
Endpoint assembly
A service combines the configured endpoint with an operation path. It places resource identifiers in that path and puts operation values in the request payload. Required values are checked before transport, so invalid service input fails at the service boundary.
The client exposes encodePath for safe path segments. It URL-encodes identifiers and rejects . and .., which prevents a path value from changing the intended resource path.
Read operations send payload values as URL query parameters. Write operations send structured values in a body. This lets one client support list reads, resource changes, and actions across different HTTP methods.
References: src/client.ts• L273-279 src/client.ts• L930-997 src/service.ts• L13-25
Shared request context
The client merges its SDK and authentication headers with headers supplied by the service. It adds an origin that identifies the React Native platform. Service methods include the configured project in their request headers, while the configured environment selects the project environment when one is in use.
The client can carry session, bearer token, JWT, developer key, cookie, locale, environment, and user impersonation context. Requests include credentials for cookie-based session use. Header details and failure behavior are covered in Request transport and error handling.
| Context | Effect on the request |
|---|---|
| Project | Routes the operation to an Appwrite project |
| Environment | Selects the project environment for supported Appwrite Cloud projects |
| Platform | Identifies the React Native application origin |
| Session or cookie | Carries user session context |
| Bearer token or JWT | Carries token-based authentication context |
| Developer key | Carries explicit developer authorization |
| Locale | Selects the requested locale for supported responses |
| Impersonation value | Requests an operation in the context of a selected user |
References: src/client.ts• L298-304 src/client.ts• L328-573 src/client.ts• L937-957
Payload serialization
The client selects a wire form from the HTTP method and content type. GET values become URL parameters. JSON operations become a JSON body. File operations use multipart form data so metadata and file content travel in one request.
Service.flatten changes nested values into bracketed keys before query encoding. Arrays become repeated bracketed fields. Service file helpers provide byte data for file parts when an upload needs data that the React Native network layer can read. The SDK uses a large-number-aware JSON parser and serializer so supported values do not lose precision during request or response conversion.
References: src/client.ts• L1-46 src/client.ts• L225-251 src/client.ts• L937-997 src/service.ts• L13-48
Network delivery
The shared client delivers the assembled request through React Native’s fetch surface. Ordinary HTTP calls do not add a separate queue or retry layer, so the returned promise represents the Appwrite exchange or its first described failure.
File uploads can add a service-level transfer flow for files larger than one multipart request. The Storage service sends byte ranges as separate requests and keeps the upload identity across those parts. The broader transport behavior is covered in Request transport and error handling.
References: src/client.ts• L1000-1077 src/service.ts• L4-5
Response and failure contract
The shared client gives service callers one result boundary for Appwrite responses. It converts structured data, preserves binary data when requested, and turns failed exchanges into AppwriteException values.
Response conversion
JSON responses become JavaScript values. A service that requests an array buffer receives binary data. Other non-JSON bodies become message objects, which gives callers a readable result when Appwrite does not return JSON.
The client also stores fallback cookies when Appwrite returns them in a browser-capable runtime. Structured results receive a JSON-backed string form, while Appwrite warning headers are sent to the console without changing the result. The client module also provides model-default support so modeled values can receive missing defaults without sharing mutable default objects between results.
References: src/client.ts• L1-119 src/client.ts• L1000-1069
Error boundary
An Appwrite response with status 400 or higher becomes an AppwriteException. The error carries the message, status code, server error type, and response text. Network and response-processing failures are wrapped in the same type, while an existing AppwriteException passes through unchanged.
Application code can handle service failures at one boundary and inspect message, code, type, and response without needing to identify whether the failure came from Appwrite or the local transport path.
References: src/client.ts• L254-270 src/client.ts• L1000-1077
Realtime relationship
Realtime uses the client’s project, environment, endpoint, platform, and session context through a WebSocket channel rather than the ordinary HTTP path. The same client state therefore affects both service calls and live event connections.
The realtime endpoint is derived from the main endpoint unless an explicit WebSocket endpoint is set. Subscriptions start the connection, and the client sends authentication and subscription messages after the socket reports that it is connected.
References: src/client.ts• L328-370 src/client.ts• L575-870 src/client.ts• L872-927
Change checklist
When these files or sections are changed, remember to consider these:
- If client configuration keys or header names change, check service project and environment headers, HTTP authentication, realtime authentication, and push authentication together.
- If endpoint handling changes, check service URL construction, WebSocket endpoint derivation, and push broker selection.
- If path encoding rules change, check every service resource path and preserve rejection of
.and..path segments. - If a service changes content type or response mode, check the matching serializer and returned value type.
- If nested or array payload rules change, check both GET query encoding and multipart field encoding.
- If file-part construction changes, check both React Native fetch inputs and chunked upload behavior.
- If model defaults change, check response shape compatibility and ensure default objects and arrays are not shared between results.
- If error fields or status handling change, preserve the shared
AppwriteExceptioncontract. - If session or environment updates change, check cookie fallback behavior, realtime authentication, and push credential selection.
- If push client identity changes, check broker replay behavior across app restarts.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The client has no project ID | Project-scoped requests lack valid project context, and realtime cannot create a valid project connection | Set the project before using services or realtime |
| A service omits a required input | The call fails before transport and no HTTP request is sent | Supply all required service parameters |
| A path value is not encoded | IDs with reserved URL characters can address the wrong resource | Use encodePath for resource IDs |
A path value is . or .. | The client rejects the call instead of allowing an unsafe path segment | Use a valid resource ID |
| A multipart request keeps a fixed content type boundary | Appwrite may not parse the file form body | Let the runtime set the multipart boundary |
| A file part does not provide readable bytes | A React Native upload path cannot construct the file body | Keep the file part’s URI and byte reader valid for the active fetch runtime |
| A binary service uses the normal response mode | The caller receives text or a message object instead of binary data | Keep the service response mode aligned with transport |
| A request returns status 400 or higher | The service promise rejects with AppwriteException | Handle the shared error fields at the application boundary |
| HTTP and realtime endpoints use different configuration rules | Calls and subscriptions can reach different or invalid server addresses | Keep endpoint override and derived WebSocket behavior aligned |
| A project environment is set for only one channel | HTTP calls and realtime subscriptions can address different project environments | Apply environment configuration consistently before creating services or subscriptions |
| The push endpoint or client ID is invalid or unstable | Push connections can use the wrong broker or fail to resume its replay cursor | Use a valid push endpoint and a stable client ID when replay across restarts is required |
Key files
| File | Why to read it |
|---|---|
| src/client.ts | Defines client state, path safety, request assembly, serialization, response conversion, error handling, model defaults, and realtime context |
| src/service.ts | Defines the shared service contract, query flattening, and file-part helpers used by transport |
| src/index.ts | Shows the public client and service exports used by React Native applications |
| src/…/account.ts | Shows a typical authenticated service operation from input checks to an Appwrite call |
| src/…/databases.ts | Shows list and record operations that use paths, queries, and JSON payloads |
| src/…/storage.ts | Shows multipart file transfer and binary response paths that depend on shared transport |
| src/…/realtime.ts | Shows how client project, endpoint, platform, environment, and session context reaches the WebSocket channel |
| src/…/push.ts | Shows how the client’s push endpoint and client ID support broker connection and replay behavior |