@usehercules/sdk
v1.17.0
Published
API for the Hercules platform
Downloads
21,078
Readme
hercules
This library provides convenient access to the hercules REST API from TypeScript or JavaScript.
The full API of this library can be found in api.md.
Contents
- Installation
- Usage
- API Reference
- Authentication
- Errors
- Client Options
- Request Options
- Retries and Timeouts
- Pagination
- Helpers
- Logging
- Requirements
Installation
npm install @usehercules/sdkUsage
import Hercules from '@usehercules/sdk';
const client = new Hercules({
apiKey: process.env['HERCULES_API_KEY'], // defaults to the HERCULES_API_KEY env var
});
const customer = await client.commerce.customers.get('REPLACE_ME');
console.log(customer);The examples in the following sections assume a client configured as shown above.
See the API reference for every available operation.
Authentication
Pass credentials to the generated client constructor. Environment variables are read automatically when supported by the target runtime.
| Option | Type | Default | Description |
| --- | --- | --- | --- |
| apiKey | string \| provider | - | Credential for the bearerAuth scheme. Defaults to HERCULES_API_KEY. |
Declared schemes:
bearerAuthbearer token
Errors
Non-success responses throw generated API errors. Error objects expose status, headers, response body, and request metadata where the target runtime supports it.
import { APIError } from '@usehercules/sdk';
try {
const customer = await client.commerce.customers.get('REPLACE_ME');
} catch (err) {
if (err instanceof APIError) {
console.log(err.status, err.name, err.headers);
}
throw err;
}Documented error statuses: 400, 402, 403, 404, 409, 422.
Client Options
Configure the generated client by setting any of these options when you create it.
import Hercules from '@usehercules/sdk';
const client = new Hercules({
timeout: 60000,
maxRetries: 2,
logLevel: 'debug',
});| Option | Type | Default | Description |
| --- | --- | --- | --- |
| apiKey | string \| AuthTokenProvider | process.env["HERCULES_API_KEY"] | Credential for the bearerAuth scheme. |
| baseURL | string \| null | process.env["HERCULES_BASE_URL"] | Override the default API base URL. Pass null when selecting a configured environment. |
| timeout | number | 60000 | Maximum time in milliseconds to wait for a response before aborting a request. |
| maxRetries | number | 2 | Number of retries for temporary failures. |
| defaultHeaders | HeadersInit | - | Headers sent with every request. |
| defaultQuery | Record<string, string \| undefined> | - | Query parameters sent with every request. |
| fetchOptions | RequestInit | - | Additional fetch options sent with every request. |
| fetch | Fetch | - | Custom fetch implementation. |
| logLevel | "off" \| "error" \| "warn" \| "info" \| "debug" \| null | process.env["HERCULES_LOG"] | Controls request and retry debug logging. |
| logger | Logger \| null | console | Custom logger implementation. |
Request Options
| Option | Type | Default | Description |
| --- | --- | --- | --- |
| headers | HeadersInit | - | Per-request headers. |
| query | Record<string, unknown> | - | Per-request query parameters. |
| body | unknown | - | Override the generated request body. |
| timeout | number | - | Per-request timeout in milliseconds. |
| maxRetries | number | - | Per-request retry count. |
| signal | AbortSignal | - | Abort an in-flight request. |
| fetchOptions | RequestInit | - | Per-request fetch options. |
| idempotencyKey | string | - | Idempotency key for retry-safe operations. Applies to this request and its retries. |
Retries and Timeouts
Generated clients support request timeouts and retry temporary failures such as network errors, 408, 409, 429, and 5xx responses. Retry delays honor Retry-After headers when present. Tune the retry and timeout client options shown above, or override them per request.
Pagination
List endpoints return paginated results you can iterate directly; the SDK fetches subsequent pages for you.
const page = await client.commerce.customers.list({
starting_after: 'id_123',
limit: 100,
});Helpers
- Use
.withResponse()on any request to inspect both parsed data and the rawResponseobject. - Every operation returns an
APIPromise, so you canawaitit directly or chain.withResponse().
Logging
- Set
logLevel: "debug"to log request URLs, options, response status, response headers, and retry attempts. - Pass a custom
loggerto route logs into your own observability pipeline. - Set
logLevel: nullto disable environment-driven logging.
Requirements
- Node.js 20+, a modern browser, or any runtime with
fetchsupport
Powered by Scalar.
