ApiClient
TheApiClient class is a lightweight HTTP client for interacting with the YouVersion Platform API using the native fetch API. It provides convenient methods for GET, POST, and DELETE requests with typed responses, automatic timeout handling, and error management.
All specialized clients (BibleClient, LanguagesClient, HighlightsClient) use this client internally.
Constructor
Parameters
Configuration object for the API client
Example
Properties
The configuration object passed to the constructor. Publicly accessible.
Methods
get()
Sends a GET request to the specified API path with optional query parameters.The API endpoint path relative to the base URL (e.g.,
/v1/bibles).Optional query parameters to include in the request.Type:
Record<string, string | number | boolean | Array<string | number | boolean>>Optional additional headers to include in the request.Type:
Record<string, string>Promise resolving to the typed response data.
Example
post()
Sends a POST request to the specified API path with optional data and query parameters.The API endpoint path relative to the base URL.
Optional request body data to send as JSON.Type:
Record<string, string | number | boolean | object>Optional query parameters to include in the request URL.
Promise resolving to the typed response data.
Example
delete()
Sends a DELETE request to the specified API path with optional query parameters.The API endpoint path relative to the base URL.
Optional query parameters to include in the request.
Promise resolving to the response data (may be void for 204 responses).
Example
Request Features
Automatic Headers
The client automatically includes these headers in every request:Content-Type: application/jsonX-YVP-App-Key: <your-app-key>X-YVP-Installation-Id: <installation-id>
Timeout Handling
Requests automatically timeout after the configured duration (default 10 seconds):Error Handling
HTTP errors are thrown asError objects with additional properties:
NODE_ENV=development), error messages include detailed server responses. In production, error messages are sanitized.
Query Parameter Arrays
Array values in query parameters are automatically expanded:Type Definitions
ApiConfig
client.ts:1
QueryParams
client.ts:4
RequestData
client.ts:5
RequestHeaders
client.ts:6
Complete Example
Best Practices
Reuse client instances
Reuse client instances
Create a single
ApiClient instance and reuse it throughout your application to avoid unnecessary overhead:Use specialized clients
Use specialized clients
Instead of calling
ApiClient methods directly, use the specialized clients that provide type-safe method signatures:Handle errors appropriately
Handle errors appropriately
Always wrap API calls in try-catch blocks and handle different error scenarios:
Configure appropriate timeouts
Configure appropriate timeouts
Adjust timeout based on expected response times:
Related
- BibleClient - Bible data operations
- LanguagesClient - Language data
- HighlightsClient - User highlights
