0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Cursorエディタ】OpenAI API Key認証で「Invalid OpenAI API Key」エラー。有料枠への支払い設定移行で解決(無料枠が終了していた)

Posted at

CursorでOpenAI APIを使用するために、APIキーを認証しようとしたところ、以下、添付画のエラーが発生しました。

結論、OpenAI API Keyの利用が無料枠のまま使用しており、有料枠への移行と支払い設定が出来ていなかったことが原因でした。

スクリーンショット 2025-02-11 16.08.05.png

ダイアログに表示されたエラーメッセージ

Invalid OpenAI API Key

Please try again, or run the following command in your terminal to verify that you have not hit your spend limit:

エラーメッセージと同時に示唆されたcurlコマンドを実行してみる。
(API_KEYは実際のものに置き換えます)

curl https://api.openai.com/v1/chat/completions -H "Content-Type: application/json" \
-H "Authorization: Bearer {API_KEY}" -d '{
  "messages": [
    {
      "role": "system",
      "content": "You are a test assistant."
    },
    {
      "role": "user",
      "content": "Testing. Just say hi and nothing else."
    }
  ],
  "model": "gpt-4o-mini"
}'

返却されたレスポンス

{
    "error": {
        "message": "You exceeded your current quota, please check your plan and billing details.",
        "type": "insufficient_quota",
        "param": null,
        "code": "insufficient_quota"
    }
}

レスポンス内容に含まれるエラーから、APIの使用量(クォータ)を超過したため、リクエストが拒否されていることがわかりました。

Open AIアカウントのダッシュボードからLimitsを確認する。
https://platform.openai.com/settings/organization/limits

current_free.png

画像の下半分、Increasing your limitsの箇所より、無料枠アカウントのまま使用していたことがわかりました。

「At least $5 spent on the API since account creation」

の記載より、無料枠から有料枠の利用に移行するためには最低支払い額が5ドル必要で、アカウント作成後にそちらの支払いが確認されない場合、使用可能な機能や利用枠が制限される、もしくは有料機能にアップグレードできない可能性があると解釈し、Buy creditsリンクをクリックして、遷移先のBillingページより、クレジットカードの登録へ進みました。

5ドル分の支払いを追加すると、先ほどのLimitsのページより、現在の使用枠がFree(無料)ではなく、tier 1という有料枠に移行できたことが確認できました。

usage_tier1.png

有料枠への移行が完了したので、もう一度先ほどのcurlを実行してみると以下のようにエラーを含まない有効なレスポンスが返ってきました。

{
  "id": "chatcmpl-xxxxxxxxx",
  "object": "chat.completion",
  "created": 1739262328,
  "model": "gpt-4o-mini-2024-07-18",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Hi!",
        "refusal": null
      },
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 26,
    "completion_tokens": 3,
    "total_tokens": 29,
    "prompt_tokens_details": {
      "cached_tokens": 0,
      "audio_tokens": 0
    },
    "completion_tokens_details": {
      "reasoning_tokens": 0,
      "audio_tokens": 0,
      "accepted_prediction_tokens": 0,
      "rejected_prediction_tokens": 0
    }
  },
  "service_tier": "default",
  "system_fingerprint": "fp_1234xxx"
}

Cursorを開き直して、APIキーのVerifyを再度行ったところ無事成功しました。

スクリーンショット 2025-02-11 17.25.47.png

使用モデルにgpt-4oを選択して回答を出力したところ、CursorアカウントのUsageからPremium modelsのリクエスト回数が期待通りに減っていることも確認ができました。

スクリーンショット 2025-02-15 15.03.54.png

スクリーンショット 2025-02-15 15.05.21.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?