Install the package, then set your API key. The SDK reads EXPAND_API_KEY from the environment by default.
export EXPAND_API_KEY="xpnd_..."
import { ExpandClient } from '@expandai/sdk'const expand = new ExpandClient({ apiKey: process.env.EXPAND_API_KEY,})
The SDK sends your key on every request as the x-expand-api-key header.
Option
Default
Description
apiKey
process.env.EXPAND_API_KEY
Expand API key.
baseUrl
https://api.expand.ai
API base URL.
Method index
Method
Use when
Returns
fetchJson(params, options?)
App code needs Main Markdown, State JSON, snapshot metadata, or inline Highlights.
Object-mode Fetch result.
fetch(params, options?)
You only need the Markdown string.
string
JSON Mode with fetchJson
fetchJson() is the recommended SDK method for applications. It returns the Main Markdown, State JSON, snapshot metadata, and optional search results in one object.
Prefer the server-provided Playground host, fall back if missing.
SDK API responses do not already contain citationUrl. MCP adds that field; SDK users build it with these helpers. See Playground & Replay for URL semantics.
Use ExpandService when your application is already Effect-native. It exposes the same operations as ExpandClient, but failures are typed in the Effect error channel.
import { NodeRuntime } from '@effect/platform-node'import { Effect } from 'effect'import { ExpandService } from '@expandai/sdk'const program = Effect.gen(function* () { const expand = yield* ExpandService const page = yield* expand.fetchJson({ url: 'https://example.com' }) yield* Effect.log(page.markdown)})NodeRuntime.runMain(
You do not need this section to use the SDK from plain Promise code.
Errors and retries
Promise-side errors, thrown by ExpandClient:
Error
Meaning
ExpandClientError
Base Promise-side SDK error and invalid client options.
ExpandClientApiError
Non-2xx API response. Inspect status and body.
ExpandClientConnectionError
Network failure.
Effect-side errors, surfaced in the ExpandService error channel:
Error
Meaning
ExpandSdkError
Invalid service options or SDK setup error.
ExpandApiError
Non-2xx API response.
ExpandConnectionError
Network failure.
ExpandTimeoutError
Retry and timeout behavior:
Default timeoutMs is 60000.
Default maxRetries is 2.
Retryable failures include timeouts, connection errors, 408, 409, 429, and 5xx.