API Client
API Client in JavaScript Method Implementations
JavaScript method implementations receive an apiClient
parameter that can be used to make requests to the external API. The API client is automatically configured with authentication and base URL settings from the connector configuration.
Making Requests
The API client provides methods for making HTTP requests:
interface ApiClient {
// GET request with optional query parameters
get(path: string, query?: Record<string, any>, options?: RestApiClientOptions): Promise<any>
// POST request with data
post(path: string, data?: any, options?: RestApiClientOptions): Promise<any>
// PUT request with data
put(path: string, data?: any, options?: RestApiClientOptions): Promise<any>
// PATCH request with data
patch(path: string, data?: any, options?: RestApiClientOptions): Promise<any>
// DELETE request with optional data
delete(path: string, data?: any, options?: RestApiClientOptions): Promise<any>
}
Examples
// GET request with query parameters
const response = await apiClient.get('/items', {
page: 1,
limit: 10
})
// POST request with data
const newItem = await apiClient.post('/items', {
name: 'New Item',
description: 'Description'
})
// PUT request to update
await apiClient.put('/items/123', {
name: 'Updated Item'
})
// DELETE request
await apiClient.delete('/items/123')
Request Options
Each request method accepts an optional options
parameter that can override the default client configuration:
interface RestApiClientOptions {
// Base URI for relative paths
baseUri?: string
// Headers to add to the request
headers?: Record<string, string>
// Query parameters to add to all requests
query?: Record<string, string>
// Basic auth credentials
auth?: {
username: string
password: string
}
// Return full response instead of just data
returnFullResponse?: boolean
// Response type - how to parse the response
responseType?: 'json' | 'arraybuffer' | 'stream'
}
Example with Options
// Get JSON response (default)
const jsonResponse = await apiClient.get('/items', {}, {
headers: {
'Accept': 'application/json'
}
})
// Download file as stream
const fileResponse = await apiClient.get('/files/123', {}, {
responseType: 'stream',
returnFullResponse: true
})
Error Handling
The API client automatically handles common error cases:
- 401 responses trigger an ACCESS_TOKEN_EXPIRED error that will attempt to refresh credentials
- 429 responses trigger a RATE_LIMIT_EXCEEDED error that will retry the request with exponential backoff (only in asynchronous mode, e.g., within a flow run)
- Other error responses (status >= 300) throw a ConnectionError
Response
By default, the API client:
- Returns the response body for successful requests (status 200–299)
- Throws an error for failed requests
- Handles authentication and rate limiting errors automatically
You can get the full response including status and headers by setting returnFullResponse: true
in the options.
Updated 16 days ago