Skip to content

TypeScript SDK ​

The official SDK for the OneFive Commerce API. Fully typed, zero runtime dependencies, works in Node.js 18+, Bun, and Deno.

bash
npm install @onefive/hydra

Initialize the client ​

typescript
import { createClient } from '@onefive/hydra'

const hydra = createClient({ apiKey: 'sk_...' })

The client accepts these options:

OptionDefaultDescription
apiKey(required)Your API key (sk_* or pk_*)
baseUrlhttps://api.hydrajs.devAPI base URL
timeout80000Request timeout in milliseconds
maxNetworkRetries1Max automatic retries on network/server errors

Both timeout and maxNetworkRetries can be overridden per-request.

Basic operations ​

Every resource follows the same pattern: list, get, create, update, delete.

List resources ​

typescript
const { data: products, pagination } = await hydra.products.list({
  limit: 25,
  status: 'active',
  expand: ['variants', 'images']
})

console.log(products.length, pagination.has_more)

Get a single resource ​

typescript
const { data: product } = await hydra.products.get('prod_abc123', {
  expand: ['variants', 'images']
})

Create a resource ​

typescript
const { data: product } = await hydra.products.create({
  title: 'Classic T-Shirt',
  status: 'active',
  variants: [
    { title: 'Small', price: 2500, sku: 'TSHIRT-S' },
    { title: 'Medium', price: 2500, sku: 'TSHIRT-M' }
  ]
})

Update a resource ​

typescript
const { data: updated } = await hydra.products.update('prod_abc123', {
  title: 'Premium T-Shirt'
})

Delete a resource ​

typescript
await hydra.products.delete('prod_abc123')

Pagination ​

List endpoints use cursor-based pagination. Every list resource has iterate() and toArray() methods built in.

Async iteration ​

typescript
for await (const product of hydra.products.iterate({ status: 'active' })) {
  console.log(product.title)
}

Collect into an array ​

typescript
const allProducts = await hydra.products.toArray({ status: 'active' })

A safety cap of 10,000 items prevents unbounded memory growth. Pass { limit: N } to override:

typescript
const first100 = await hydra.products.toArray(
  { status: 'active' },
  { limit: 100 }
)

Error handling ​

The SDK throws typed error classes for different failure modes:

typescript
import {
  HydraNotFoundError,
  HydraValidationError,
  HydraRateLimitError,
  HydraAuthenticationError
} from '@onefive/hydra'

try {
  await hydra.products.get('prod_nonexistent')
} catch (err) {
  if (err instanceof HydraNotFoundError) {
    // 404 - resource doesn't exist
  } else if (err instanceof HydraValidationError) {
    // 400 - invalid request body
    console.log(err.message)
  } else if (err instanceof HydraRateLimitError) {
    // 429 - too many requests
  } else if (err instanceof HydraAuthenticationError) {
    // 401 - invalid API key
  }
}

All error classes extend HydraError, which has status, message, and code properties.

Error classHTTP statusWhen it's thrown
HydraAuthenticationError401Invalid or missing API key
HydraPermissionError403Key lacks permission for this operation
HydraNotFoundError404Resource doesn't exist
HydraValidationError400Request body fails validation
HydraRateLimitError429Rate limit or quota exceeded
HydraConnectionError-Network failure or timeout

Idempotency ​

POST requests can include an idempotency key to prevent duplicate creates:

typescript
const { data: product } = await hydra.products.create(body, {
  idempotencyKey: 'unique-key-123'
})

When maxNetworkRetries is greater than 0, the SDK auto-generates idempotency keys for POST requests to make retries safe.

Available resources ​

ResourceProperty
Productshydra.products
Imageshydra.images
Storehydra.store
Deploy Hookshydra.deployHooks
API Keyshydra.apiKeys

TypeScript types ​

All API resource types are exported for use in your application:

typescript
import type { Product, Variant, Image } from '@onefive/hydra'