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

# 最初のリクエストを実行する

> 数分で X API の利用を開始できます

export const Button = ({href, children}) => {
  return <div className="not-prose group">
    <a href={href}>
      <button className="flex items-center space-x-2.5 py-1 px-4 bg-primary-dark dark:bg-white text-white dark:text-gray-950 rounded-full group-hover:opacity-[0.9] font-medium">
        <span>
          {children}
        </span>
        <svg width="3" height="24" viewBox="0 -9 3 24" class="h-6 rotate-0 overflow-visible"><path d="M0 0L3 3L0 6" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"></path></svg>
      </button>
    </a>
  </div>;
};

このガイドでは、最初の X API リクエストの実行手順を順を追って説明します。始める前に、[App の認証情報を持つ開発者アカウント](/ja/x-api/getting-started/getting-access)が必要です。

***

<div id="quick-start-with-curl">
  ## cURL を使ったクイックスタート
</div>

API をテストする最も手軽な方法は cURL を使うことです。ユーザーを取得してみましょう。

```bash theme={null}
curl "https://api.x.com/2/users/by/username/xdevelopers" \
  -H "Authorization: Bearer $BEARER_TOKEN"
```

`$BEARER_TOKEN` を実際のベアラートークンに置き換えてください。次のようなレスポンスが返ってきます:

```json theme={null}
{
  "data": {
    "id": "2244994945",
    "name": "X Developers",
    "username": "xdevelopers"
  }
}
```

***

<div id="step-by-step-guide">
  ## ステップバイステップガイド
</div>

<Steps>
  <Step title="ベアラートークンを取得する">
    [開発者コンソール](https://console.x.com)で対象の App を開き、ベアラートークンをコピーします。
  </Step>

  <Step title="エンドポイントを選択する">
    まずは次のような初心者向けのエンドポイントから始めましょう。

    | Endpoint                                             | 機能                          |
    | :--------------------------------------------------- | :-------------------------- |
    | [User lookup](/ja/x-api/users/lookup/introduction)   | ユーザー名または ID からユーザープロフィールを取得 |
    | [Post lookup](/ja/x-api/posts/lookup/introduction)   | ID でポストを取得                  |
    | [Recent search](/ja/x-api/posts/search/introduction) | 直近 7 日間の投稿を検索               |
  </Step>

  <Step title="リクエストを実行する">
    cURL、Postman、または任意の HTTP クライアントを使用します。

    ```bash theme={null}
    # ユーザー名からユーザーを取得
    curl "https://api.x.com/2/users/by/username/xdevelopers" \
      -H "Authorization: Bearer $BEARER_TOKEN"
    ```
  </Step>

  <Step title="レスポンスを解析する">
    レスポンスは JSON 形式です。主なデータは `data` フィールドに含まれます。

    ```json theme={null}
    {
      "data": {
        "id": "2244994945",
        "name": "X Developers",
        "username": "xdevelopers"
      }
    }
    ```
  </Step>
</Steps>

***

<div id="request-more-data-with-fields">
  ## フィールドでより多くのデータをリクエストする
</div>

デフォルトではエンドポイントは最小限のフィールドのみを返します。追加のデータをリクエストするには、`fields` パラメータを使用します。

```bash theme={null}
curl "https://api.x.com/2/users/by/username/xdevelopers?user.fields=created_at,description,public_metrics" \
  -H "Authorization: Bearer $BEARER_TOKEN"
```

レスポンス:

```json theme={null}
{
  "data": {
    "id": "2244994945",
    "name": "X Developers",
    "username": "xdevelopers",
    "created_at": "2013-12-14T04:35:55.000Z",
    "description": "X Developer Platformの声",
    "public_metrics": {
      "followers_count": 570842,
      "following_count": 2048,
      "tweet_count": 14052,
      "listed_count": 1672
    }
  }
}
```

[フィールドの詳細はこちら →](/ja/x-api/fundamentals/fields)

***

<div id="more-examples">
  ## その他の例
</div>

<Tabs>
  <Tab title="ポストを取得">
    ```bash theme={null}
    curl "https://api.x.com/2/tweets/1460323737035677698?tweet.fields=created_at,public_metrics" \
      -H "Authorization: Bearer $BEARER_TOKEN"
    ```
  </Tab>

  <Tab title="最近の投稿を検索">
    ```bash theme={null}
    curl "https://api.x.com/2/tweets/search/recent?query=from:xdevelopers&tweet.fields=created_at" \
      -H "Authorization: Bearer $BEARER_TOKEN"
    ```
  </Tab>

  <Tab title="ユーザーの投稿を取得">
    ```bash theme={null}
    curl "https://api.x.com/2/users/2244994945/tweets?max_results=5" \
      -H "Authorization: Bearer $BEARER_TOKEN"
    ```
  </Tab>
</Tabs>

***

<div id="using-code-instead-of-curl">
  ## cURL の代わりにコードを使用する
</div>

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    import requests

    bearer_token = "YOUR_BEARER_TOKEN"
    url = "https://api.x.com/2/users/by/username/xdevelopers"

    headers = {"Authorization": f"Bearer {bearer_token}"}
    response = requests.get(url, headers=headers)

    print(response.json())
    ```
  </Tab>

  <Tab title="JavaScript">
    ```javascript theme={null}
    const bearerToken = "YOUR_BEARER_TOKEN";
    const url = "https://api.x.com/2/users/by/username/xdevelopers";

    fetch(url, {
      headers: { Authorization: `Bearer ${bearerToken}` }
    })
      .then(res => res.json())
      .then(data => console.log(data));
    ```
  </Tab>

  <Tab title="公式 SDK">
    本番環境での利用には、公式 SDK の使用を推奨します。

    * [Python SDK](/ja/xdks/python/overview)
    * [TypeScript SDK](/ja/xdks/typescript/overview)

    これらは認証、ページネーション、レート制限を自動的に処理します。
  </Tab>
</Tabs>

***

<div id="tools-for-testing">
  ## テスト用ツール
</div>

<CardGroup cols={3}>
  <Card title="Postman" icon="server" href="/ja/tutorials/postman-getting-started">
    コレクションを使用したビジュアルなAPIテスト。
  </Card>

  <Card title="サンプルコード" icon="github" href="https://github.com/xdevplatform/Twitter-API-v2-sample-code">
    複数言語でのサンプルコード。
  </Card>

  <Card title="APIリファレンス" icon="code" href="/ja/x-api/posts/lookup/introduction">
    各エンドポイントの詳細なドキュメント。
  </Card>
</CardGroup>

***

<div id="troubleshooting">
  ## トラブルシューティング
</div>

<Accordion title="401 Unauthorized">
  * ベアラートークンが正しいことを確認してください
  * トークンが再発行されていないことを確認してください
  * `Authorization` ヘッダーの形式を確認してください: `Bearer YOUR_TOKEN`
</Accordion>

<Accordion title="403 Forbidden">
  * App がこのエンドポイントにアクセスできない可能性があります
  * 一部のエンドポイントでは、ユーザーコンテキストでの認証 (OAuth 1.0a または 2.0) が必要です
  * 開発者コンソールで App の権限を確認してください
</Accordion>

<Accordion title="429 Too Many Requests">
  * レート制限に達しています
  * 再試行可能な時刻については、`x-rate-limit-reset` ヘッダーを確認してください
  * コード内で指数バックオフを実装してください
</Accordion>

[エラーの詳細なリファレンス →](/ja/x-api/fundamentals/response-codes-and-errors)

***

<div id="next-steps">
  ## 次のステップ
</div>

<CardGroup cols={2}>
  <Card title="認証について学ぶ" icon="key" href="/ja/resources/fundamentals/authentication/overview">
    ユーザーコンテキストのリクエストにおける OAuth を理解しましょう。
  </Card>

  <Card title="エンドポイントを探る" icon="compass" href="/ja/x-api/posts/search/introduction">
    どのようなものを構築できるかを確認しましょう。
  </Card>

  <Card title="SDK を使う" icon="cube" href="/ja/xdks/overview">
    公式ライブラリで開発を高速化しましょう。
  </Card>

  <Card title="何かを作ってみる" icon="hammer" href="/ja/x-api/what-to-build">
    作成できるもののアイデアを見つけましょう。
  </Card>
</CardGroup>
