> ## Documentation Index
> Fetch the complete documentation index at: https://x-preview-mintlify-066e8699.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeScript XDK

> Install and use the official TypeScript XDK client library for the X API v2, with authentication, typed responses, pagination, and streaming examples.

The [TypeScript XDK](https://github.com/xdevplatform/twitter-api-typescript-sdk) is the official client library for the X API v2. Full type safety, automatic pagination, and event-driven streaming.

<Card title="GitHub repository" icon="github" href="https://github.com/xdevplatform/twitter-api-typescript-sdk">
  Source code, issues, and releases.
</Card>

***

## Installation

<CodeGroup>
  ```bash npm theme={null}
  npm install @xdevplatform/xdk
  ```

  ```bash yarn theme={null}
  yarn add @xdevplatform/xdk
  ```

  ```bash pnpm theme={null}
  pnpm add @xdevplatform/xdk
  ```
</CodeGroup>

Requires Node.js 16+ and TypeScript 4.5+ (if using TypeScript).

***

## Quick start

```typescript theme={null}
import { Client } from '@xdevplatform/xdk';

const client = new Client({ bearerToken: 'YOUR_BEARER_TOKEN' });

const userResponse = await client.users.getByUsername('XDevelopers');
console.log(userResponse.data?.username);
```

***

## Key features

| Feature                  | Description                                                      |
| :----------------------- | :--------------------------------------------------------------- |
| **Type safety**          | Complete TypeScript definitions for all endpoints and parameters |
| **Authentication**       | Bearer Token, OAuth 2.0 with PKCE, and OAuth 1.0a                |
| **Automatic pagination** | Async iteration support for paginated endpoints                  |
| **Streaming**            | Event-driven streaming with automatic reconnection               |
| **Full API coverage**    | Users, Posts, Lists, Bookmarks, Communities, and more            |

***

## Authentication

<Tabs>
  <Tab title="Bearer Token">
    ```typescript theme={null}
    import { Client } from '@xdevplatform/xdk';

    const client = new Client({ bearerToken: 'YOUR_BEARER_TOKEN' });
    ```
  </Tab>

  <Tab title="OAuth 2.0">
    ```typescript theme={null}
    import { Client, OAuth2, generateCodeVerifier, generateCodeChallenge } from '@xdevplatform/xdk';

    const oauth2 = new OAuth2({
      clientId: 'YOUR_CLIENT_ID',
      clientSecret: 'YOUR_CLIENT_SECRET',
      redirectUri: 'https://your-app.com/callback',
      scope: ['tweet.read', 'users.read', 'offline.access'],
    });

    const codeVerifier = generateCodeVerifier();
    const codeChallenge = await generateCodeChallenge(codeVerifier);
    oauth2.setPkceParameters(codeVerifier, codeChallenge);
    const authUrl = await oauth2.getAuthorizationUrl('state');

    const tokens = await oauth2.exchangeCode(authCode, codeVerifier);
    const client = new Client({ accessToken: tokens.access_token });
    ```
  </Tab>

  <Tab title="OAuth 1.0a">
    ```typescript theme={null}
    import { Client, OAuth1 } from '@xdevplatform/xdk';

    const oauth1 = new OAuth1({
      apiKey: 'YOUR_API_KEY',
      apiSecret: 'YOUR_API_SECRET',
      accessToken: 'YOUR_ACCESS_TOKEN',
      accessTokenSecret: 'YOUR_ACCESS_TOKEN_SECRET'
    });

    const client = new Client({ oauth1: oauth1 });
    ```
  </Tab>
</Tabs>

***

## Common methods

| Category   | Method                           |
| :--------- | :------------------------------- |
| **Posts**  | `client.posts.search()`          |
| **Users**  | `client.users.getMe()`           |
| **Spaces** | `client.spaces.findSpaceById()`  |
| **Lists**  | `client.lists.getList()`         |
| **DMs**    | `client.directMessages.lookup()` |

***

## Learn more

<CardGroup cols={2}>
  <Card title="Installation" icon="download" href="/xdks/typescript/install">
    Package managers, TypeScript setup, and requirements.
  </Card>

  <Card title="Authentication" icon="key" href="/xdks/typescript/authentication">
    Detailed guide for all auth methods.
  </Card>

  <Card title="Pagination" icon="arrows-left-right" href="/xdks/typescript/pagination">
    Async iteration and paginated responses.
  </Card>

  <Card title="Streaming" icon="satellite-dish" href="/xdks/typescript/streaming">
    Event-driven streaming with reconnection.
  </Card>

  <Card title="API Reference" icon="book" href="/xdks/typescript/reference/modules">
    Complete client, interface, and type reference.
  </Card>
</CardGroup>

For code examples, see the [samples repo](https://github.com/xdevplatform/samples/tree/main/javascript).
