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

# TypeScript XDK

> X API v2 向け公式 TypeScript XDK クライアントライブラリのインストールと使用方法。認証、型付きレスポンス、ページネーション、ストリーミングの例を含みます。

[TypeScript XDK](https://github.com/xdevplatform/twitter-api-typescript-sdk) は X API v2 向けの公式クライアントライブラリです。完全な型安全性、自動ページネーション、イベント駆動のストリーミングを提供します。

<Card title="GitHub リポジトリ" icon="github" href="https://github.com/xdevplatform/twitter-api-typescript-sdk">
  ソースコード、Issue、リリース。
</Card>

***

## インストール

<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>

Node.js 16 以上および TypeScript 4.5 以上 (TypeScript を使用する場合) が必要です。

***

## クイックスタート

```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);
```

***

## 主な機能

| 機能                | 説明                                            |
| :---------------- | :-------------------------------------------- |
| **型安全性**          | すべてのエンドポイントとパラメーターに対する完全な TypeScript 定義       |
| **認証**            | Bearer Token、PKCE 付き OAuth 2.0、および OAuth 1.0a |
| **自動ページネーション**    | ページネーション対応エンドポイントで非同期イテレーションをサポート             |
| **ストリーミング**       | 自動再接続を備えたイベント駆動型ストリーミング                       |
| **完全な API カバレッジ** | Users、Posts、Lists、Bookmarks、Communities ほか    |

***

## 認証

<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>

***

## よく使うメソッド

| カテゴリ       | メソッド                             |
| :--------- | :------------------------------- |
| **Posts**  | `client.posts.search()`          |
| **Users**  | `client.users.getMe()`           |
| **Spaces** | `client.spaces.findSpaceById()`  |
| **Lists**  | `client.lists.getList()`         |
| **DMs**    | `client.directMessages.lookup()` |

***

## さらに詳しく

<CardGroup cols={2}>
  <Card title="インストール" icon="download" href="/xdks/typescript/install">
    パッケージマネージャー、TypeScript のセットアップ、必要条件。
  </Card>

  <Card title="認証" icon="key" href="/xdks/typescript/authentication">
    すべての認証方法の詳細ガイド。
  </Card>

  <Card title="ページネーション" icon="arrows-left-right" href="/xdks/typescript/pagination">
    非同期イテレーションとページネーションレスポンス。
  </Card>

  <Card title="ストリーミング" icon="satellite-dish" href="/xdks/typescript/streaming">
    再接続付きイベント駆動型ストリーミング。
  </Card>

  <Card title="API リファレンス" icon="book" href="/xdks/typescript/reference/modules">
    クライアント、インターフェース、型の完全リファレンス。
  </Card>
</CardGroup>

コード例については [samples リポジトリ](https://github.com/xdevplatform/samples/tree/main/javascript) を参照してください。
