---
title: "TypeScript SDK"
description: "`@groundsocial/sdk`: typed calls, paging, retries and the cost of every call."
canonical_url: https://groundsocial.dev/docs/sdk
---
# TypeScript SDK

> `@groundsocial/sdk`: typed calls, paging, retries and the cost of every call.

> ## Agent Instructions
> The Groundsocial REST API is at https://api.groundsocial.dev (OpenAPI: https://groundsocial.dev/openapi.json). Set yourself up with https://groundsocial.dev/SKILL.md; the MCP server is https://api.groundsocial.dev/mcp. Every docs page is Markdown at its URL plus `.md`; the index is https://groundsocial.dev/llms.txt.

> **Not on npm yet** The SDK, CLI and stdio MCP server are not published yet. Until they are, use the REST API or the remote MCP server, and don't install packages named `@groundsocial/…` from npm: they aren't ours.

```bash
npm i @groundsocial/sdk
```

## Create a client

```ts
import { createClient } from '@groundsocial/sdk'

const client = createClient({ apiKey: process.env.GROUNDSOCIAL_API_KEY })
```

| Option | Default | What it does |
| --- | --- | --- |
| `apiKey` | `GROUNDSOCIAL_API_KEY` | Your key. Pass `null` for keyless mode (pay per call with `paymentHandler`). |
| `baseUrl` | `GROUNDSOCIAL_API_URL`, then the public API | Where to send calls. |
| `sessionToken` | none | A dashboard session, for key management (a key can’t manage keys). |
| `maxRetries` | 2 | Retries for 429, 5xx and network errors, honouring `Retry-After`. |
| `timeoutMs` | 60000 | Per attempt. |
| `userAgent` | none | Identifies your app in our logs. |
| `onResponse` | none | Called after every call with its cost: `creditsCharged`, `creditsRemaining`, `requestId`, `attempts`. |
| `paymentHandler` | none | Answers a 402 by paying (MPP or x402) and returning the headers to retry with. |
| `headers`, `fetch` | none | Extra headers; a custom fetch. |

Keys are only sent over https or to a loopback address, unless you set `allowInsecureTransport`.

## Reddit methods

| Method | Returns |
| --- | --- |
| `reddit.subreddit(name)` | A subreddit (`t5`). |
| `reddit.subredditPosts(name, { sort, t, limit })` | Listing of posts. |
| `reddit.post(id, { sort, limit })` | `{ post, comments }`. |
| `reddit.moreComments(postId, children, { sort })` | The expanded comments of a `more` item. |
| `reddit.user(name)` | A user (`t2`). |
| `reddit.userPosts(name)` / `reddit.userComments(name)` | Listings. |
| `reddit.searchPosts(q, { subreddit, sort, t })` | Listing of posts. |
| `reddit.searchSubreddits(q)` | Listing of subreddits. |

Responses are Reddit’s own Thing and Listing JSON, typed from the OpenAPI document. `client.call(operationId, input)` calls any operation by id with one flat input object and returns `{ status, data, meta }`.

## Paging

Listing methods return a `ListingPromise`. Await it for one page; iterate it for more. Nothing is fetched or charged until you do one of the two, and each page is one call.

```ts
const page = await client.reddit.subredditPosts('typescript', { sort: 'new' })

for await (const post of client.reddit.subredditPosts('typescript').items({ maxItems: 250 })) {}
for await (const page of client.reddit.userComments('quiet_ops').pages({ maxPages: 5 })) {}
```

## Errors

Failures throw `ApiError` with `code`, `status`, `message`, `hint`, `docsUrl`, `retryable`, `requestId`, `details` and `retryAfterSeconds`. Use `isApiError(err)` to narrow. See [Errors](https://groundsocial.dev/docs/errors).

## Accounts and signup

- `client.account()`: credits left and which key is calling. Free.
- `client.onboarding.signup({ agentName, purpose })`: agent self-signup. Returns `{ status: "created", api_key, … }` or `{ status: "challenge", challenge }`; call again with `challengeToken` and `answer`.
- `client.onboarding.deviceStart(clientName)` and `devicePoll(deviceCode)`: device login.
- `client.onboarding.emailStart(email)`, `emailVerify(email, code)`, `me()`, `listKeys`, `createKey`, `revokeKey`: dashboard calls with a session token.

## Examples

TypeScript:

```ts
import { createClient, isApiError } from '@groundsocial/sdk'

const client = createClient({ apiKey: process.env.GROUNDSOCIAL_API_KEY })

try {
  for await (const post of client.reddit.searchPosts('self-hosted backups').items({ maxItems: 100 })) {
    console.log(post.data.subreddit, post.data.title)
  }
} catch (err) {
  if (isApiError(err)) console.error(err.code, err.hint)
}
```

_Canonical HTML: https://groundsocial.dev/docs/sdk_

## Site Navigation

### Global Hub
- [Home](https://groundsocial.dev/index.md)
- [Pricing](https://groundsocial.dev/pricing.md)
- [Endpoints](https://groundsocial.dev/endpoints.md)
- [For agents](https://groundsocial.dev/agents.md)
- [Docs](https://groundsocial.dev/docs.md)
- [Compare](https://groundsocial.dev/compare.md)
- [Changelog](https://groundsocial.dev/changelog.md)
- [llms.txt](https://groundsocial.dev/llms.txt)
- [Agent skill](https://groundsocial.dev/SKILL.md)

### On This Topic
- [Groundsocial docs](https://groundsocial.dev/docs.md)
- [Quickstart](https://groundsocial.dev/docs/quickstart.md)
- [Authentication](https://groundsocial.dev/docs/authentication.md)
- [Credits and payments](https://groundsocial.dev/docs/payments.md)
- [Rate limits](https://groundsocial.dev/docs/rate-limits.md)
- [Errors](https://groundsocial.dev/docs/errors.md)
- [Data freshness and caching](https://groundsocial.dev/docs/freshness.md)
- [MCP server](https://groundsocial.dev/docs/mcp.md)
- [SKILL.md, llms.txt and Markdown twins](https://groundsocial.dev/docs/skill.md)
- [CLI](https://groundsocial.dev/docs/cli.md)
- [PRAW and snoowrap](https://groundsocial.dev/docs/praw.md)
- [Legal and compliance](https://groundsocial.dev/docs/compliance.md)
- [API reference](https://groundsocial.dev/docs/api.md)

## Structured data

```json
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://groundsocial.dev/"},{"@type":"ListItem","position":2,"name":"Docs","item":"https://groundsocial.dev/docs"},{"@type":"ListItem","position":3,"name":"TypeScript SDK","item":"https://groundsocial.dev/docs/sdk"}]}
```
