Request transport and error handling
The SDK gives every service one shared path for sending requests to Appwrite. That path applies common headers and auth context, encodes inputs, converts response data, carries cookies, and turns transport or API failures into AppwriteException values. See Client configuration and request lifecycle for the parent view and Client setup and platform integration for client creation and platform setup.
Request transport
The request transport connects service methods to the Appwrite API without making each service manage HTTP rules on its own. A service builds the endpoint-specific URL and payload, then Client.call supplies the shared request behavior.
Request lifecycle
When an application calls a service method, the request moves from service inputs to an Appwrite response through a small set of shared stages:
flowchart LR
A[Service method] --> B[URL and payload]
B --> C[Shared headers and auth context]
C --> D[Query or body encoding]
D --> E[HTTP request]
E --> F[Response conversion]
F --> G[Typed service result]
F --> H[AppwriteException]
The service layer owns endpoint meaning and required fields. The client layer owns the wire format and common result or failure shape. This separation lets account, data, storage, avatar, and other services use the same transport contract.
References: src/client.ts• L937-1077 src/service.ts• L4-25 src/…/account.ts• L25-33
Header context
Each request starts with SDK headers and then applies headers supplied by the service. The client adds the platform origin after that merge so Appwrite can identify the React Native platform and configured application platform.
Client configuration adds auth and request context through headers for sessions, bearer tokens, JWTs, locale, cookies, and user impersonation. These values travel with the request so the service endpoint can apply the caller's project and user context.
The client enables credentials on every request. This allows session cookies to take part in the request path, while setCookie also supports callers that must forward an incoming cookie explicitly.
References: src/client.ts• L299-305 src/client.ts• L448-573 src/client.ts• L937-957 src/…/account.ts• L29-33
Payload encoding
The transport selects the wire form from the HTTP method and content type. A GET request places flattened values in the URL. A non-GET request uses JSON for structured data or multipart form data for files and other form fields.
The shared flatten step represents nested values with bracketed keys. Multipart arrays use repeated keys with a [] suffix. The client leaves the multipart boundary to the runtime so the file body remains valid.
| Request kind | Transport form | Main use |
|---|---|---|
| GET | URL query parameters | Filters, options, tokens, and other read inputs |
| JSON request | JSON body | Account, data, team, and other structured service actions |
| Multipart request | Form data body | File creation and upload parts |
The client uses a JSON parser and serializer that support values larger than the normal JavaScript safe integer range. This keeps large numeric fields consistent between service inputs and API responses.
References: src/client.ts• L5-46 src/client.ts• L946-997 src/service.ts• L13-25 src/…/account.ts• L93-117
Response conversion
The response content type controls the returned value. JSON responses become parsed objects, binary responses requested as arrayBuffer become ArrayBuffer values, and other responses become an object with the response text in message.
The transport also reads Appwrite warning headers and sends each warning to the console. Successful object results receive a JSON-backed toString value, which gives callers a stable text form without changing the visible response fields. The SDK can apply model defaults to response objects when a caller supplies a default tree. Missing values receive independent defaults while the response's text form remains available.
References: src/client.ts• L48-119 src/client.ts• L1000-1069 src/…/avatars.ts• L91-103 src/…/storage.ts• L570-590
File uploads
The Storage service uses the shared multipart path for file creation and adds a larger-file transfer flow on top of it. Small files use one request. Larger files use byte ranges so the SDK can transfer the file in parts and report progress.
The exported FilePart type gives a multipart file both a native URI and a byte reader. This lets React Native networking stacks use the same upload contract when one stack reads the URI and another builds the body from bytes.
Chunked upload flow
The upload flow treats the first successful part as the point that creates or resumes the server-side file. Later parts identify that upload and carry their byte range. The service can send several later parts at once while stopping the worker group when one part fails.
sequenceDiagram
participant App as Application
participant SDK as Storage service
participant API as Appwrite API
App->>SDK: createFile(file, onProgress)
alt File fits one chunk
SDK->>API: One multipart request
API-->>SDK: File result
else Larger file
SDK->>API: Check existing upload state
API-->>SDK: Uploaded offset or no state
SDK->>API: First multipart byte range
API-->>SDK: File and upload ID
loop Remaining byte ranges
SDK->>API: Multipart part with range and upload ID
API-->>SDK: Part result
SDK-->>App: Upload progress
end
SDK-->>App: Final file result
end
The transfer uses a maximum chunk size of 5 MB. Progress reports include the upload ID, percentage, uploaded byte count, total chunk count, and completed chunk count. A failed part rejects the upload promise so the caller can decide whether to retry.
References: src/client.ts• L225-244 src/service.ts• L4-5 src/…/storage.ts• L89-106 src/…/storage.ts• L178-336
Cookie handling
Cookies support two session paths: normal credential forwarding and a fallback for environments that cannot use the expected cookie setup. The client can forward a caller-provided incoming Cookie header, which supports server-side request contexts.
When Appwrite returns fallback cookie data in X-Fallback-Cookies, the client stores it in browser local storage and warns that a custom API domain gives stronger session security. Realtime authentication can later use the configured session or this stored fallback session.
References: src/client.ts• L514-527 src/client.ts• L937-957 src/client.ts• L1049-1060 src/client.ts• L730-779
SDK errors
The SDK exposes one error type for failures that reach service callers. AppwriteException carries the readable message, HTTP status code, server error type, and the raw or serialized response text.
Error boundary
Service methods reject missing required inputs before transport. The shared client then handles two failure classes:
- An Appwrite response with status 400 or higher becomes an
AppwriteExceptionbuilt from the response data. - A fetch or response-processing failure becomes an
AppwriteExceptionbuilt from the underlying error message.
Existing AppwriteException values pass through unchanged. Callers can therefore inspect code, type, response, and message without knowing whether the error came from a service parameter check or the HTTP exchange.
flowchart TD
A[Service call] --> B{Required input present?}
B -- No --> C[AppwriteException]
B -- Yes --> D[HTTP request]
D --> E{Status below 400?}
E -- Yes --> F[Converted result]
E -- No --> G[AppwriteException with API details]
D --> H[Network or parse failure]
H --> I[AppwriteException with local message]
References: src/client.ts• L254-270 src/client.ts• L937-943 src/client.ts• L1000-1077 src/…/account.ts• L76-92 src/…/storage.ts• L146-161
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the header names used by client configuration aligned with the auth and project headers expected by every service.
- Preserve the agreement between response content types, the
responseTypevalue, and the result type exposed by binary services. - Keep multipart boundary handling with the runtime when changing upload headers or request-body assembly.
- Keep file-part fields usable by both React Native and Expo networking stacks when changing the shared upload type.
- Keep chunk size, byte-range rules, upload IDs, and progress fields aligned across the Storage upload flow and the Appwrite upload contract.
- Preserve the error fields and pass-through behavior when changing status handling so callers can still distinguish API failures from local transport failures.
- Review cookie fallback behavior with both normal request credentials and realtime session authentication when changing session handling.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
A service sends a binary response without requesting arrayBuffer | The response is treated as text or a message object instead of binary data | Keep the service response type and transport responseType aligned |
| Multipart content type is sent with a fixed boundary | The server may not parse the form body | Let the runtime set the multipart boundary |
| A file part does not provide the field needed by the active networking stack | The upload body may not contain readable file data | Keep both the URI and byte reader available on FilePart values |
| A large upload part uses the wrong byte range or upload ID | The API can reject the part or attach it to the wrong upload | Preserve byte ranges and the server-issued upload ID for later parts |
| A request receives an HTTP status of 400 or higher | The service promise rejects with AppwriteException rather than a normal result | Inspect code, type, response, and message at the caller boundary |
| A required service input is absent | No HTTP request is sent | Provide the required input before invoking the service method |
| The runtime cannot use the expected cookie flow | Session state may rely on local storage fallback and its associated security warning | Configure a suitable custom API domain when cookie security matters |
Key files
| File | Why to read it |
|---|---|
| src/client.ts | Defines the shared request boundary, headers, payload encoding, response conversion, cookie fallback, response defaults, file-part contract, and AppwriteException. |
| src/service.ts | Defines the common payload flattening rule and the upload chunk size used by service layers. |
| src/…/storage.ts | Shows the file upload, binary download, preview, and view paths that depend on transport conversion. |
| src/…/account.ts | Shows a typical service request that supplies project headers and a JSON payload to the shared client. |
| src/…/avatars.ts | Shows a binary image response returned through the shared arrayBuffer path. |
| src/index.ts | Exposes Client, AppwriteException, UploadProgress, and the service APIs to SDK callers. |