メインコンテンツへスキップ
レート制限は、各エンドポイントごとに実行できるリクエスト数を制御します。制限を超過すると、ウィンドウがリセットされるまで 429 エラーが返されます。

レート制限の仕組み

概念説明
時間ウィンドウ通常は 15 分または 24 時間です
ユーザー単位の制限OAuth 1.0a または OAuth 2.0 のユーザートークンを使用する場合に適用されます
App 単位の制限ベアラートークン (App 専用) を使用する場合に適用されます
エンドポイント単位各エンドポイントごとに固有の制限があります

制限の確認

レスポンスヘッダーには、現在のレート制限の状態が示されます。
x-rate-limit-limit: 900
x-rate-limit-remaining: 847
x-rate-limit-reset: 1705420800
HeaderDescription
x-rate-limit-limit許可される最大リクエスト数
x-rate-limit-remainingウィンドウ内で残っているリクエスト数
x-rate-limit-resetウィンドウがリセットされる時刻の Unix タイムスタンプ

レート制限テーブル

各エンドポイントのレート制限は以下のテーブルで確認できます。開発者コンソールでも同じ制限を確認できます。
特に記載がない限り、制限は 15 分ごとの上限として表示されています (例: 「/24hrs」や「/sec」) 。

投稿 (25個のエンドポイント)

ツイートの取得

MethodEndpointApp ごとユーザーごと
GET/2/tweets3,500/15min5,000/15min
GET/2/tweets/:id450/15min900/15min
MethodEndpointPer AppPer UserNotes
GET/2/tweets/search/recent450/15min300/15minデフォルトは 10 件、最大 100 件の結果; クエリ長は最大 512 文字
MethodEndpointPer AppPer UserNotes
GET/2/tweets/search/all1/sec, 300/15min1/secデフォルト 10 件、最大 500 件の結果; クエリ長 1024 文字

ポスト数

MethodEndpointPer AppPer User注記
GET/2/tweets/counts/recent300/15minクエリ長: 512
GET/2/tweets/counts/all300/15minクエリ長: 1024

フィルタ済みストリーム

MethodEndpointApp ごとユーザーごと注記
GET/2/tweets/search/stream50/15min接続数 1; ルール数 1000; ルール長 1024; 250 投稿/秒
GET/2/tweets/search/stream/rules450/15min接続数 1; ルール数 1000; ルール長 1024
POST/2/tweets/search/stream/rules100/15min接続数 1; ルール数 1000; ルール長 1024

投稿の管理

MethodEndpointApp ごとユーザーごと
POST/2/tweets10,000/24hrs100/15min
DELETE/2/tweets/:id50/15min

タイムライン

MethodEndpointApp ごとユーザーごと
GET/2/users/:id/tweets10,000/15min900/15min
GET/2/users/:id/mentions450/15min300/15min
GET/2/users/:id/timelines/reverse_chronological180/15min

いいねの取得

MethodEndpointApp ごとユーザーごと
GET/2/tweets/:id/liking_users75/15min75/15min
GET/2/users/:id/liked_tweets75/15min75/15min

いいね管理

メソッドエンドポイントApp 単位ユーザー単位
POST/2/users/:id/likes50/15分, 1,000/24時間
DELETE/2/users/:id/likes/:tweet_id50/15分, 1,000/24時間

リツイートの取得

MethodEndpointPer AppPer UserNotes
GET/2/tweets/:id/retweeted_by75/15min75/15min
GET/2/tweets/:id/quote_tweets75/15min75/15min
GET/2/users/reposts_of_me75/15min最大 100 件の結果

リツイートの管理

メソッドエンドポイントアプリごとユーザーごと
POST/2/users/:id/retweets50/15min
DELETE/2/users/:id/retweets/:tweet_id50/15min

返信を非表示にする

MethodEndpointPer AppPer User
PUT/2/tweets/:tweet_id/hidden50/15min

ユーザー (14個のエンドポイント)

ユーザー取得

MethodEndpointApp ごとユーザーごと
GET/2/users300/15min900/15min
GET/2/users/:id300/15min900/15min
GET/2/users/by300/15min900/15min
GET/2/users/by/username/:username300/15min900/15min
GET/2/users/me75/15min

ユーザー検索

MethodEndpointApp ごとユーザーごと
GET/2/users/search300/15min900/15min

フォロー取得

MethodEndpointApp ごとユーザーごと
GET/2/users/:id/following300/15min300/15min
GET/2/users/:id/followers300/15min300/15min

フォローの管理

メソッドエンドポイントApp ごとユーザーごと
POST/2/users/:id/following50/15min
DELETE/2/users/:source_user_id/following/:target_user_id50/15min

ブロックの取得

MethodEndpointApp ごとユーザーごと
GET/2/users/:id/blocking15/15min

ミュートの取得

MethodEndpointPer AppPer User
GET/2/users/:id/muting15/15min

ミュートの管理

MethodEndpointApp 単位ユーザー単位
POST/2/users/:id/muting50/15min
DELETE/2/users/:source_user_id/muting/:target_user_id50/15min

Spaces (6 個のエンドポイント)

Spaces 取得

MethodEndpointアプリごとユーザーごと
GET/2/spaces/:id300/15min300/15min
GET/2/spaces300/15min300/15min
GET/2/spaces/:id/tweets300/15min300/15min
GET/2/spaces/by/creator_ids300/15min, 1/sec300/15min, 1/sec
GET/2/spaces/:id/buyers300/15min300/15min

Spaces の検索

MethodEndpointPer AppPer User
GET/2/spaces/search300/15分300/15分

ダイレクトメッセージ (8個のエンドポイント)

ダイレクトメッセージの取得

MethodEndpointApp ごとユーザーごと
GET/2/dm_events15/15min
GET/2/dm_events/:id15/15min
GET/2/dm_conversations/:dm_conversation_id/dm_events15/15min
GET/2/dm_conversations/with/:participant_id/dm_events15/15min

ダイレクトメッセージの管理

MethodEndpointアプリごとユーザーごと
POST/2/dm_conversations1,440/24hrs15/15min, 1,440/24hrs
POST/2/dm_conversations/with/:participant_id/messages1,440/24hrs15/15min, 1,440/24hrs
POST/2/dm_conversations/:dm_conversation_id/messages1,440/24hrs15/15min, 1,440/24hrs
DELETE/2/dm_events/:id4,000/24hrs300/15min, 1,500/24hrs

リスト (14 個のエンドポイント)

リストの取得

MethodEndpointApp ごとユーザーごと
GET/2/lists/:id75/15min75/15min
GET/2/users/:id/owned_lists15/15min15/15min

リスト内ツイートの取得

メソッドエンドポイントApp ごとユーザーごと
GET/2/lists/:id/tweets900/15min900/15min

リストメンバーの参照

メソッドエンドポイントApp ごとユーザーごと
GET/2/lists/:id/members900/15min900/15min
GET/2/users/:id/list_memberships75/15min75/15min

リストの管理

MethodEndpointPer AppPer User
POST/2/lists300/15min
DELETE/2/lists/:id300/15min
PUT/2/lists/:id300/15min

リストのメンバーを管理

MethodEndpointPer AppPer User
POST/2/lists/:id/members300/15min
DELETE/2/lists/:id/members/:user_id300/15min

リストフォローの管理

MethodEndpointApp ごとユーザーごと
POST/2/users/:id/followed_lists50/15min
DELETE/2/users/:id/followed_lists/:list_id50/15min

ピン留めされたリスト

MethodEndpointPer AppPer User
GET/2/users/:id/pinned_lists15/15min15/15min
POST/2/users/:id/pinned_lists50/15min
DELETE/2/users/:id/pinned_lists/:list_id50/15min

ブックマーク (5個のエンドポイント)

ブックマークの取得

メソッドエンドポイントApp ごとユーザーごと
GET/2/users/:id/bookmarks180/15min
GET/2/users/:id/bookmarks/folders50/15min50/15min
GET/2/users/:id/bookmarks/folders/:folder_id50/15min50/15min

ブックマークの管理

MethodEndpointPer AppPer User
POST/2/users/:id/bookmarks50/15min
DELETE/2/users/:id/bookmarks/:tweet_id50/15min

コンプライアンス (3 個のエンドポイント)

バッチコンプライアンス

メソッドエンドポイントApp ごとユーザーごと
POST/2/compliance/jobs150/15min
GET/2/compliance/jobs/:job_id150/15min
GET/2/compliance/jobs150/15min

使用量 (1 エンドポイント)

メソッドエンドポイントApp ごとユーザーごと
GET/2/usage/tweets50/15分

MethodEndpointPer AppPer User
GET/2/users/personalized_trends200/24hrs, 200/15min100/24hrs, 10/15min
MethodEndpointApp ごとユーザーごと
GET/2/trends/by/woeid/:id75/15min

Communities (2 エンドポイント)

MethodEndpointApp ごとユーザーごと注記
GET/2/communities/:id300/15min300/15min
GET/2/communities/search300/15min300/15min最大 100 件の結果

アナリティクス (1 個のエンドポイント)

MethodEndpointApp ごとユーザーごと
GET/2/tweets/analytics300/15min300/15min

メディア (8 エンドポイント)

メソッドエンドポイントApp ごとユーザーごと
POST/2/media/upload50,000/24時間500/15分
GET/2/media/upload100,000/24時間1,000/15分
POST/2/media/upload/initialize180,000/24時間1,875/15分
POST/2/media/upload/:id/append180,000/24時間1,875/15分
POST/2/media/upload/:id/finalize180,000/24時間1,875/15分
POST/2/media/metadata50,000/24時間500/15分
POST/2/media/subtitles10,000/24時間100/15分
DELETE/2/media/subtitles10,000/24時間100/15分

アクティビティと Webhook

MethodEndpointApp ごとユーザーごと備考
GET/2/activity/stream450/15分同時接続2件、1秒あたり250投稿
POST/2/activity/subscriptions500/15分
GET/2/activity/subscriptions500/15分
PUT/2/activity/subscriptions/:subscription_id500/15分
DELETE/2/activity/subscriptions/:subscription_id500/15分
POST/2/webhooks450/15分
GET/2/webhooks450/15分
PUT/2/webhooks/:webhook_id450/15分
DELETE/2/webhooks/:webhook_id450/15分
POST/2/webhooks/replay100/15分

その他のエンドポイント

MethodEndpointAppごとユーザーごと
GET/2/tweets/sample10/stream100/15min
GET/2/news/:id200/15min
GET/2/news/search200/15min200/15min
POST/2/users/:id/dm/block25/15min, 1,000/24hrs10/15min, 400/24hrs
POST/2/users/:id/dm/unblock25/15min, 1,000/24hrs10/15min, 400/24hrs
GET/2/users/by/username/:username/tweets1,500/15min900/15min
GET/2/users/by/username/:username/mentions450/15min180/15min
GET/2/users/:id/following/spaces300/15min300/15min
GET/2/tweets/:id/retweets75/15min75/15min
DELETE/2/connections/all25/15min25/15min

レート制限への対処

レート制限に達すると、HTTP 429 レスポンスが返されます。
{
  "errors": [{
    "code": 88,
    "message": "Rate limit exceeded"
  }]
}

リカバリー戦略

  1. x-rate-limit-reset ヘッダーでウィンドウのリセット時刻を確認する
  2. 再試行する前にその時刻まで待機する
  3. 必要に応じて指数バックオフを使用する
import time

def make_request_with_backoff(url, headers):
    response = requests.get(url, headers=headers)
    
    if response.status_code == 429:
        reset_time = int(response.headers.get('x-rate-limit-reset', 0))
        wait_time = max(reset_time - time.time(), 60)
        time.sleep(wait_time)
        return make_request_with_backoff(url, headers)
    
    return response

ベストプラクティス

レスポンスをキャッシュする

結果をローカルに保存して、同じリクエストの繰り返しを減らします。

ストリーミングを利用する

リアルタイムデータには、ポーリングではなくフィルタ済みストリームを使用します。

ヘッダーを監視する

残りリクエスト数を追跡し、レート制限に達するのを避けます。

リクエストを分散する

リクエストを時間ウィンドウ全体に分散させます。

レート制限と課金

レート制限と課金は別に管理されています。
概念目的
レート制限システムの安定性を保つためにリクエスト頻度を制御する
利用課金取得したデータ量に応じて課金する (従量課金)
レート制限内に収まっていても利用料金が発生しますし、追加の料金なしでレート制限に達することもあります。

エンタープライズ向けのレート制限

エンタープライズのお客様には、カスタムのレート制限が設定されています。アカウントマネージャーに連絡するか、Enterprise へのアクセスを申請してください。

次のステップ

エラー処理

429 などのエラーを処理します。

はじめに

アクセスレベルと機能について学びます。