> ## Documentation Index
> Fetch the complete documentation index at: https://docs.autoposting.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeScript SDK

> Official TypeScript SDK for the Autoposting.ai API — zero dependencies, fully typed.

## Install

```bash theme={null}
npm install @autoposting.ai/sdk
```

Zero dependencies — uses native `fetch` (Node.js 20+). ESM and CJS builds included.

## Quick Start

```typescript theme={null}
import { Autoposting } from '@autoposting.ai/sdk'

const client = new Autoposting({ apiKey: 'sk-social-...' })

// Create and publish a post
const post = await client.posts.create({
  brandSlug: 'my-brand',
  text: 'Hello from the SDK!',
  platforms: ['x', 'linkedin'],
})

await client.posts.publish(post.id)
```

## Configuration

```typescript theme={null}
const client = new Autoposting({
  apiKey: 'sk-social-...',          // required
  baseUrl: 'https://app.autoposting.ai/api-proxy', // optional — this is the default (the /api-proxy path; the bare host serves the dashboard, not the API)
  timeout: 30000,                   // optional — request timeout in ms
})
```

## Resources

| Resource            | Methods                                                                                                                     |
| ------------------- | --------------------------------------------------------------------------------------------------------------------------- |
| `client.posts`      | `list` · `getById` · `create` · `update` · `remove` · `publish` · `schedule` · `unschedule` · `retry` · `rewrite` · `score` |
| `client.media`      | `upload`                                                                                                                    |
| `client.brands`     | `list` · `retrieve` · `create` · `update` · `remove` · `authStatus`                                                         |
| `client.agents`     | `list` · `retrieve` · `create` · `update` · `remove` · `run` · `toggle` · `runs`                                            |
| `client.kb`         | `list` · `retrieve` · `create` · `remove` · `search` · `ingestUrl` · `docs`                                                 |
| `client.ideas`      | `list` · `generate` · `enrich` · `remove`                                                                                   |
| `client.clips`      | `list` · `retrieve` · `importUrl` · `render` · `remove`                                                                     |
| `client.carousels`  | `list` · `retrieve` · `create` · `generate` · `draft` · `remove`                                                            |
| `client.webhooks`   | `list` · `retrieve` · `create` · `update` · `remove` · `test`                                                               |
| `client.billing`    | `status` · `credits`                                                                                                        |
| `client.usage`      | `summary`                                                                                                                   |
| `client.workspaces` | `list` · `switchWorkspace`                                                                                                  |

## Usage Examples

### Post Scheduling

```typescript theme={null}
const posts = await client.posts.list({ status: 'scheduled' })

const post = await client.posts.create({
  brandSlug: 'my-brand',
  text: 'Check out our latest blog post!',
  platforms: ['x', 'linkedin', 'threads'],
  scheduledAt: '2026-02-01T14:00:00Z',
})

// AI-rewrite for better engagement
await client.posts.rewrite(post.id)

// Get AI score and feedback
const score = await client.posts.score(post.id)
```

### AI Agents

```typescript theme={null}
const agent = await client.agents.create({
  name: 'Daily Tech News',
  type: 'publish',
  brandSlug: 'my-brand',
  prompt: 'Write a short post about the latest AI news',
  frequency: 'daily',
  time: '09:00',
})

await client.agents.run(agent.id)
const runs = await client.agents.runs(agent.id)
```

### Knowledge Base

```typescript theme={null}
const kb = await client.kb.create({ name: 'Product Docs' })
await client.kb.ingestUrl(kb.id, 'https://docs.example.com')
const results = await client.kb.search(kb.id, 'pricing')
```

### Carousels

```typescript theme={null}
const carousel = await client.carousels.generate({
  topic: '5 Tips for Social Media Growth',
  brandSlug: 'my-brand',
  slideCount: 5,
})

await client.carousels.draft(carousel.id)
```

### Webhooks

```typescript theme={null}
const webhook = await client.webhooks.create({
  url: 'https://api.example.com/webhook',
  events: ['post.published', 'post.failed'],
})

await client.webhooks.test(webhook.id)
```

## Error Handling

The SDK throws typed errors you can catch by class:

```typescript theme={null}
import {
  AutopostingError,     // base class
  AuthenticationError,  // 401 — invalid API key
  ScopeError,           // 403 — insufficient API key scopes
  NotFoundError,        // 404 — resource not found
  ValidationError,      // 400/422 — invalid input
  RateLimitError,       // 429 — rate limited
  ServerError,          // 5xx — server error
} from '@autoposting.ai/sdk'

try {
  await client.posts.getById('nonexistent')
} catch (err) {
  if (err instanceof NotFoundError) {
    console.log('Post not found:', err.message)
  }
  if (err instanceof RateLimitError) {
    console.log('Rate limited, retry after:', err.message)
  }
}
```

## Auth Priority (when using with CLI)

```
--api-key flag  >  AUTOPOSTING_API_KEY env  >  stored credentials
```

<CardGroup cols={2}>
  <Card title="API Reference" icon="code" href="/api-reference/overview">
    Full endpoint reference with auth and error codes.
  </Card>

  <Card title="MCP Server" icon="robot" href="/mcp/overview">
    Let AI agents use the SDK via 51 MCP tools.
  </Card>

  <Card title="CLI" icon="terminal" href="/cli/overview">
    Terminal access with 61 commands.
  </Card>

  <Card title="npm" icon="npm" href="https://www.npmjs.com/package/@autoposting.ai/sdk">
    View on npm registry.
  </Card>
</CardGroup>
