# TypeScript SDK

> Source: https://parallelworks.com/docs/sdk/typescript

# TypeScript SDK

`@parallelworks/client` is the official TypeScript client for the ACTIVATE platform API.

## Installation

```bash
npm install @parallelworks/client
```

For SWR hooks support (React):

```bash
npm install @parallelworks/client swr swr-openapi
```

## Quick Start

For CI or another unattended job, supply an [API key](/docs/account-settings/authentication#managing-api-keys) through `PW_API_KEY`. The SDK extracts its platform host. For work you run yourself, including long-running scripts, use [your saved CLI sign-in](#signed-in-with-pw-auth) instead:

```typescript

const client = Client.fromCredential(process.env.PW_API_KEY!)

const { data, error } = await client.GET('/api/buckets')
```

## Authentication

### Signed in with `pw auth`

For scripts you run yourself, [sign in with `pw auth`](/docs/cli#sign-in), then create the client from CLI credential config. **The SDK automatically obtains OAuth access tokens, authenticates requests, and renews tokens through the CLI while your sign-in remains valid.** Your code does not need to manage tokens or refresh logic. The SDK runs `pw auth token --print -o json` when it needs a token, so keep the [CLI](/docs/cli) installed and on your `PATH`.

```typescript

const client = Client.fromCredentialConfig()

const { data } = await client.GET('/api/buckets')
```

This needs Node.js 20.16 or later. The client uses the same context as the CLI: `PW_API_KEY` if set, then the `context` option, `PW_CONTEXT`, and the current context. The `cliCommand` option sets the path to `pw`. If `pw` can't renew the sign-in, requests reject with `SignInExpiredError`; run `pw auth` again.

To select a context named `research`, use `Client.fromCredentialConfig({ context: 'research' })`. Without an explicit context, the client uses `PW_CONTEXT` or the saved default. A nonempty `PW_API_KEY` overrides even the explicitly named context, so unset it to use your saved sign-in.

For long-running work, reuse this client instead of passing a copied access token to a raw-credential constructor. `PW_CREDENTIALS_DIR` selects a separate `$PW_CREDENTIALS_DIR/.credentials` file for both the SDK and its CLI subprocess. Set it before creating the client and keep the directory writable. See [long-running clients and separate credentials](/docs/sdk#long-running-clients-and-separate-credentials).

For CI and persistent unattended services, use an API key in `PW_API_KEY` and follow the [key rotation steps](/docs/cli/authentication#unattended-jobs-and-key-rotation).

### Automatic Host Detection

These raw-credential constructors infer the host, but do not renew the credential you pass. Use the credential-config constructor above for a renewable sign-in.

An API key (`pwt_...`) contains your platform host, so `fromCredential` connects to it with no URL. An access token from `pw auth token --print` doesn't name a host: the client uses `PW_PLATFORM_HOST` if set, otherwise the server of the selected [CLI context](/docs/cli#sign-in).

```typescript
// API key: host decoded from the key
const client = Client.fromCredential('pwt_Y2xvdWQucGFyYWxsZWwud29ya3M.xxxxx')
// Connects to: https://cloud.parallel.works

// Access token from `pw auth token --print`: host from PW_PLATFORM_HOST,
// else the server of the selected context (Node.js)
const client = Client.fromCredential(token)
```

### Explicit Host

If you prefer to specify the host explicitly:

```typescript
// API key (Basic Auth) - for CI and other unattended automation
const client = new Client('https://cloud.parallel.works')
  .withApiKey('pwt_...')

// Access token (Bearer), sent as is and never renewed
const client = new Client('https://cloud.parallel.works')
  .withToken(token)

// Auto-detect credential type
const client = new Client('https://cloud.parallel.works')
  .withCredential(process.env.PW_CREDENTIAL!)
```

### Credential Helpers

```typescript

isApiKey('pwt_abc.xyz')           // true
isToken('eyJ.abc.def')            // true
extractPlatformHost('pwt_...')    // "cloud.parallel.works"
```

## SWR Hooks (React)

The client ships with optional [SWR](https://swr.vercel.app/) hooks for React applications. Keep API keys and CLI credentials on the server; do not bundle them into browser JavaScript. The example below assumes your frontend is served from the same origin as an authenticated ACTIVATE API session. An independently hosted frontend needs its own server-side authentication integration; the Node.js CLI credential provider does not run in the browser:

```tsx
// lib/api.ts

// Same-origin requests use the signed-in user's existing browser session.
const client = new Client(window.location.origin)

```

```tsx
// components/BucketList.tsx

  const { data, error, isLoading } = useQuery('/api/buckets')

  if (isLoading) return <div>Loading...</div>
  if (error) return <div>Error: {error.message}</div>

  return (
    <ul>
      {data?.map(bucket => (
        <li key={bucket.id}>{bucket.name}</li>
      ))}
    </ul>
  )
}
```

## Related Documentation

- [SDKs Overview](/docs/sdk) — Authentication basics and other languages
- [API Reference](/api) — Browse every endpoint, with schemas and examples
- [Creating API Keys](/docs/account-settings/authentication#managing-api-keys) — Generate a credential for the SDK
