> **For coding agents and LLMs:** This is one page from the Social Fetch docs (markdown export). The sections below mirror the orientation block in [`/llms.txt`](https://www.socialfetch.dev/llms.txt); use [`/llms.json`](https://www.socialfetch.dev/llms.json) when you need a structured operation inventory. The catalog covers documented operations with on-site reference pages.

## This page

- **On-site (HTML):** [https://www.socialfetch.dev/docs/sdk](https://www.socialfetch.dev/docs/sdk)
- **Markdown (.mdx) URL:** [https://www.socialfetch.dev/docs/sdk.mdx](https://www.socialfetch.dev/docs/sdk.mdx)

## API base URL and authentication

- **API origin (from OpenAPI `servers`):** `https://api.socialfetch.dev`
- **Authentication:** send `x-api-key: sfk_...` on `/v1/**` routes unless the operation is explicitly anonymous (check OpenAPI `security`, the [API reference hub](https://www.socialfetch.dev/docs/api.mdx), [`/llms.txt`](https://www.socialfetch.dev/llms.txt), or [`/llms.json`](https://www.socialfetch.dev/llms.json) for each route).
- **OpenAPI JSON:** [https://www.socialfetch.dev/openapi.json](https://www.socialfetch.dev/openapi.json)

## Recommended docs entrypoints (this site)

- [Documentation overview](https://www.socialfetch.dev/docs.mdx) — top-level orientation (markdown).
- [Quickstart](https://www.socialfetch.dev/docs/quickstart.mdx) — authenticate with `x-api-key`, validate auth with `whoami`, and understand the JSON envelope.
- [SDK](https://www.socialfetch.dev/docs/sdk.mdx) — official TypeScript SDK guide, including `SocialFetchClient`, `Result`, and `unwrap()`.
- [SDK reference](https://www.socialfetch.dev/docs/sdk-reference.mdx) — exhaustive SDK method inventory and route mapping for agents, tooling, and power users.
- [Choose the right endpoint](https://www.socialfetch.dev/docs/choose-endpoint.mdx) — task-oriented route selection for smoke tests, profiles, list endpoints, and single-item lookups.
- [Capability matrix](https://www.socialfetch.dev/docs/capability-matrix.mdx) — fast comparison of identifiers, pagination, outcomes, media download, and SDK coverage.
- [`/llms.json`](https://www.socialfetch.dev/llms.json) — structured machine-readable operation inventory with parameter names, pagination, outcomes, credits, and SDK mapping.
- [API reference hub](https://www.socialfetch.dev/docs/api.mdx) — human-friendly index of operations with links into generated pages.
- [Errors](https://www.socialfetch.dev/docs/errors.mdx) — shared error envelope and HTTP status guidance.
- [Credits](https://www.socialfetch.dev/docs/credits.mdx) — metering, `402`, and planning batch jobs.
- Outcome semantics such as `found`, `not_found`, and `private` are documented in [Errors](https://www.socialfetch.dev/docs/errors.mdx) and on operation pages when present in the OpenAPI contract.

## Markdown docs convention

- Every docs page has a markdown twin: append **`.mdx`** to the docs pathname (for example `/docs/quickstart` → `/docs/quickstart.mdx`).
- Agents that send `Accept: text/markdown` on `/docs/**` HTML URLs may receive markdown directly (same URL, `Vary: Accept`).

---
# TypeScript SDK (https://www.socialfetch.dev/docs/sdk)

Install [#install]

The package is published on [npm as `@socialfetch/sdk`](https://www.npmjs.com/package/@socialfetch/sdk).

<PackageInstall pkg="@socialfetch/sdk" />

Requirements [#requirements]

* **Node.js** `18+`
* A Social Fetch API key (`sfk_...`) — [**API Keys**](https://app.socialfetch.dev/api-keys)

Quick start [#quick-start]

```ts
import { SocialFetchClient } from "@socialfetch/sdk";

const client = new SocialFetchClient({
  apiKey: process.env.SOCIALFETCH_API_KEY!,
});

const result = await client.tiktok.getProfile({ handle: "charlidamelio" });

if (!result.ok) {
  console.error(result.error.code, result.error.requestId);
  return;
}

console.log(result.value.data.profile);
```

Client configuration [#client-configuration]

Most integrations only need `apiKey`:

* **`apiKey`** — required, sent as the `x-api-key` header

```ts
import { SocialFetchClient } from "@socialfetch/sdk";

const client = new SocialFetchClient({
  apiKey: process.env.SOCIALFETCH_API_KEY!,
});
```

Advanced options like `baseUrl` and `fetch` exist for tests and custom environments, but most integrations only need `apiKey`.

Result handling [#result-handling]

SDK methods return a `Result` instead of throwing for expected API failures:

```ts
type Result<T, E> =
  | { ok: true; value: T }
  | { ok: false; error: E };
```

That means your normal flow can branch on `result.ok`:

```ts
const result = await client.tiktok.getProfile({ handle: "charlidamelio" });

if (!result.ok) {
  console.error(result.error.code, result.error.requestId);
} else {
  console.log(result.value.data.profile);
}
```

`unwrap()` and thrown failures [#unwrap-and-thrown-failures]

If you prefer exception-style control flow, import `unwrap()`:

```ts
import { SocialFetchClient, SocialFetchUnwrapError, unwrap } from "@socialfetch/sdk";

const client = new SocialFetchClient({
  apiKey: process.env.SOCIALFETCH_API_KEY!,
});

try {
  const tweets = unwrap(
    await client.twitter.getProfileTweets({ handle: "elonmusk" })
  );

  console.log(tweets.data.tweets);
} catch (error) {
  if (error instanceof SocialFetchUnwrapError) {
    console.error(error.error.code, error.error.requestId);
  } else {
    throw error;
  }
}
```

`unwrap()` returns `result.value` on success and throws `SocialFetchUnwrapError` on SDK/API failures.

Error handling [#error-handling]

The SDK uses `SocialFetchSdkError`, which has two categories:

* **`kind: "api"`** — normalized public API errors such as `unauthorized`, `insufficient_credits`, or `temporarily_unavailable`
* **`kind: "client"`** — local/runtime failures such as `network_error`, `parse_error`, or `invalid_response`

You can also import:

* **`PUBLIC_API_ERROR_CODES`**
* **`isSocialFetchSdkError()`**

<Callout type="warn" title="Important">
  The SDK keeps the same API semantics. A route can return HTTP `200` and still resolve to `not_found` or `private`, so check fields like `lookupStatus` when the route provides them.
</Callout>

After `result.ok` [#after-resultok]

Handle SDK/API errors first, then inspect `result.value.data`:

```ts
import { SocialFetchClient } from "@socialfetch/sdk";

const client = new SocialFetchClient({
  apiKey: process.env.SOCIALFETCH_API_KEY!,
});

const profile = await client.instagram.getProfile({ handle: "instagram" });

if (!profile.ok) {
  // 401, 402, 503, parse errors, etc.
  console.error(profile.error.code, profile.error.requestId);
  return;
}

switch (profile.value.data.lookupStatus) {
  case "found":
    // use profile.value.data.profile / metrics
    break;
  case "private":
  case "not_found":
    // still HTTP 200 from the API — handle without treating as SDK failure
    break;
}
```

Some list routes omit `lookupStatus` and can return empty arrays for more than one reason. See [Errors](/docs/errors).

Recommended workflow [#recommended-workflow]

1. Start with [Quickstart](/docs/quickstart) for environment setup, your first request, and the response-envelope basics.
2. Use this page for `Result` handling and `unwrap()`.
3. Use the [API reference](/docs/api) for exact params and schemas.
4. Use [Errors](/docs/errors) and [Credits](/docs/credits) for runtime behavior.

<Cards>
  <Card title="Quickstart" description="Authenticate, make a first platform request, and choose between raw HTTP and SDK paths." href="/docs/quickstart" icon="<Zap className=&#x22;size-5 text-fd-primary&#x22; />" />

  <Card title="API reference" description="See the exact routes, parameters, and response schemas behind SDK methods." href="/docs/api" icon="<Braces className=&#x22;size-5 text-fd-primary&#x22; />" />

  <Card title="Errors" description="HTTP error handling, outcome semantics, and retry guidance still apply to SDK users." href="/docs/errors" icon="<AlertTriangle className=&#x22;size-5 text-fd-primary&#x22; />" />

  <Card title="npm package" description="Published package metadata and install source on npm." href="https://www.npmjs.com/package/@socialfetch/sdk" icon="<Package className=&#x22;size-5 text-fd-primary&#x22; />" />
</Cards>