Installation
Requirements
- Node.js 18+ (uses
fetchnatively) - TypeScript 5+ recommended
Install the package
bash
# npm
npm install @clockwork/sdk
# pnpm
pnpm add @clockwork/sdk
# yarn
yarn add @clockwork/sdkInitialize the client
typescript
import { Clockwork } from '@clockwork/sdk'
const client = new Clockwork({
apiKey: process.env.CLOCKWORK_API_KEY,
})The client constructor accepts:
| Option | Type | Required | Description |
|---|---|---|---|
apiKey | string | one of | ck_live_... key from the developer console |
sessionToken | string | () => Promise<string | null> | one of | Supabase JWT for browser use |
baseUrl | string | no | Override API base URL (default: https://platform.clockwork-co.com) |
fetch | typeof fetch | no | Custom fetch implementation |
Either apiKey or sessionToken is required. If neither is provided, the constructor throws.
TypeScript
All SDK methods are fully typed. Inputs, responses, and enums are exported from the package:
typescript
import { Clockwork } from '@clockwork/sdk'
import type { Task, CreateTaskInput, TaskStatus } from '@clockwork/sdk'Use in Next.js (server-side)
typescript
// lib/clockwork.ts
import { Clockwork } from '@clockwork/sdk'
export const clockwork = new Clockwork({
apiKey: process.env.CLOCKWORK_API_KEY!,
})Use in Next.js (client-side with Supabase auth)
typescript
import { Clockwork } from '@clockwork/sdk'
import { createClientComponentClient } from '@supabase/auth-helpers-nextjs'
const supabase = createClientComponentClient()
const client = new Clockwork({
sessionToken: async () => {
const { data } = await supabase.auth.getSession()
return data.session?.access_token ?? null
},
})Pass sessionToken as a function so the client fetches a fresh token on each request, avoiding JWT expiry issues.
Custom fetch
For environments without native fetch (Node < 18, edge runtimes with custom polyfills):
typescript
import { Clockwork } from '@clockwork/sdk'
import nodeFetch from 'node-fetch'
const client = new Clockwork({
apiKey: process.env.CLOCKWORK_API_KEY,
fetch: nodeFetch as typeof fetch,
})