{"meta":{"title":"Handling API rate limits","intro":"コパイロットチャット can help handle API rate limits by suggesting code that detects them and implements retry logic.","product":"GitHub Copilot","breadcrumbs":[{"href":"/ja/copilot","title":"GitHub Copilot"},{"href":"/ja/copilot/tutorials","title":"チュートリアル"},{"href":"/ja/copilot/tutorials/copilot-cookbook","title":"GitHub Copilot Cookbook"},{"href":"/ja/copilot/tutorials/copilot-cookbook/debug-errors","title":"Debug errors"},{"href":"/ja/copilot/tutorials/copilot-cookbook/debug-errors/handle-api-rate-limits","title":"Handle API rate limits"}],"documentType":"article"},"body":"# Handling API rate limits\n\nコパイロットチャット can help handle API rate limits by suggesting code that detects them and implements retry logic.\n\nWhen making requests to APIs, it's common to encounter rate limits that restrict the number of calls you can make within a certain time frame. ギットハブ コパイロット チャット can help you handle these limits by suggesting code to detect rate limit responses and automatically retry requests after a delay.\n\n## Example scenario\n\nThe following Python code fetches weather data from an external API. If the API has rate limits, requests may fail when limits are exceeded, and your app may need a way to handle these responses gracefully by implementing retry logic.\n\n```python id=rate-limit-example\nfrom flask import Flask, request\nimport requests\n\napp = Flask(__name__)\n\nWEATHER_API_URL = \"https://api.example.com/weather\"\n\n@app.route('/get_weather', methods=['GET'])\ndef get_weather():\n    city = request.args.get('city')\n    # Simulate an API request to the external weather service\n    response = requests.get(WEATHER_API_URL, params={\"city\": city})\n    weather_data = response.json()\n\n    return weather_data\n```\n\n## Example prompt\n\n```copilot copy prompt ref=rate-limit-example\nHow can I handle API rate limits within get_weather().\n```\n\n## Example response\n\n> \\[!NOTE] 次の応答は例です。 コパイロットチャット の応答は非決定的であるため、ここで示したものとは異なる応答が返される可能性があります。\n\nCopilot might suggest code that implements a retry mechanism with exponential backoff to limit the frequency of retry attempts.\n\nFor example:\n\n```python\nimport requests\nfrom flask import Flask, request\nfrom requests.adapters import HTTPAdapter\nfrom requests.packages.urllib3.util.retry import Retry\n\napp = Flask(__name__)\n\nWEATHER_API_URL = \"https://api.example.com/weather\"\n\ndef requests_retry_session(\n    retries=3,\n    backoff_factor=0.3,\n    status_forcelist=(500, 502, 504),\n    session=None,\n):\n    session = session or requests.Session()\n    retry = Retry(\n        total=retries,\n        read=retries,\n        connect=retries,\n        backoff_factor=backoff_factor,\n        status_forcelist=status_forcelist,\n    )\n    adapter = HTTPAdapter(max_retries=retry)\n    session.mount(\"http://\", adapter)\n    session.mount(\"https://\", adapter)\n    return session\n\n@app.route('/get_weather', methods=['GET'])\ndef get_weather():\n    city = request.args.get('city')\n    try:\n        response = requests_retry_session().get(WEATHER_API_URL, params={\"city\": city})\n        response.raise_for_status()\n        weather_data = response.json()\n    except requests.exceptions.RequestException as e:\n        return {\"error\": str(e)}, 500\n\n    return weather_data\n```\n\nIn this example, Copilot's suggestions include setting up a retry session that allows the code to automatically retry requests if they fail due to specific status codes (500, 502, 504). The `backoff_factor` gradually increases the delay between retries, helping avoid exceeding the API's rate limit further.\n\n## Further reading\n\n* [GitHub Copilot Chat のプロンプト エンジニアリング](/ja/copilot/using-github-copilot/prompt-engineering-for-github-copilot)\n* [GitHub Copilot の使用に関するベスト プラクティス](/ja/copilot/using-github-copilot/best-practices-for-using-github-copilot)"}