How-To

SendGrid authorization grant invalid, expired or revoked: fix

SendGrid returns this 401 when it rejects your credential: a deleted, truncated or wrong-account API key, a malformed Authorization header, or a restricted account. Test the key against the scopes endpoint, then replace it if it fails, and check the account status if it does not.

Sohail HussainSohail Hussain9 min read

Short answer

  • SendGrid is rejecting the credential you sent: the API key was deleted, mistyped, truncated or has lost permissions.
  • Create a new key under Settings > API Keys, send it as a Bearer token (or as the SMTP password with the username apikey), and redeploy.
  • If a fresh key still fails, check IP Access Management, account status and plan credits.

You call SendGrid and get this back:

The provided authorization grant is invalid, expired, or revoked

On the Web API it arrives as an HTTP 401. Over SMTP it shows up as 535 Authentication failed: The provided authorization grant is invalid, expired, or revoked. It sounds like OAuth, but you are not in an OAuth flow. SendGrid is saying it does not accept the key, or the account behind it, for this request.

Why it happens

SendGrid's troubleshooting table lists this 401 as a key that is invalid, was deleted, or had its permissions changed, and a key in an environment variable that suddenly stops working. That covers most cases. The same table lists other 401s that look similar, so check the message text carefully.

CauseWhat you seeFix
Key deleted or never copied fullyThe grant message on every callCreate a new key and replace it everywhere
Key was exposed and auto-deletedWorked yesterday, 401 todayCreate a new key and keep it out of public repos
Key permissions changedSome endpoints work, others failEdit the key's scopes
Wrong header or SMTP usernameFails on first try with a new keyUse Bearer, or apikey as SMTP username
Whitespace or line breaks in the valueKey looks right but failsRe-paste and trim the value
IP Access Management enabledThe requestor's IP address is not whitelistedAdd your server IP to the allow list
Account disabledAccount DisabledReply to the email SendGrid sent, or open a ticket
Plan credits used upMaximum credits exceededUpgrade or wait for the reset

The last three rows are different messages on the same 401 status. Rotating the key will not fix them.

Step-by-step fixes

1. Test the key outside your app

Run one request with the key you think is live. The scopes endpoint touches no data.

curl -i https://api.sendgrid.com/v3/scopes \
  -H "Authorization: Bearer $SENDGRID_API_KEY"

A 200 with a list of scopes means the key is good and your app is sending something different (wrong variable, stale deploy, bad header). A 401 means SendGrid rejects the key itself. One caveat: SendGrid's reference says this endpoint's own 401 means the caller isn't authorized to view the scopes, so read the message text and not just the status code.

2. Check that the key still exists

  1. Open Settings > API Keys in the SendGrid console.
  2. Look for the key by name. SendGrid says the key must be available there and have the right permissions for the endpoint you call.
  3. If it is gone, someone deleted it. SendGrid can't retrieve or restore an unknown or deleted key.
  4. Check your inbox for a notice about an exposed key. SendGrid may automatically delete an exposed API key to protect your account, and requests using the deleted key then fail with a 401.

3. Create a replacement key

  1. Go to Settings > API Keys and click Create API Key.
  2. Name it after the app that will use it.
  3. Pick the least privilege that works; a Restricted Access key limits a key to the permissions you select. For sending only, the key needs Mail Send > Full Access.
  4. Click Create & View and copy the key right away. The console displays the key only once.
  5. Put it in your secrets store or environment variable, then restart or redeploy. Many apps read variables only at startup.

Do not paste the key into tickets or chat

Anyone with the key can send as your account. Share only the first few characters when you ask for help, and delete any key you pasted somewhere public.

4. Fix the header format (Web API)

SendGrid expects an Authorization header with the value Bearer followed by your key. Common mistakes:

  • No Bearer prefix, or two spaces after it.
  • The key sent as Basic auth or in a query string.
  • Quotes or a trailing newline inside the environment variable.
  • A .env file your deployment never loaded, leaving an empty or old value.

Log the length of the variable, never the value, and compare it with the key you created. SendGrid's SMTP guide describes the password as the full 69-character key. A shorter length means truncation, and a longer one means extra characters.

5. Fix the SMTP settings

For SMTP, the 535 version of this error has its own checklist from SendGrid. Work through these:

  1. Host: smtp.sendgrid.net.
  2. Username: the literal string apikey. It is the exact string "apikey" and not the API key itself.
  3. Password: the full API key, not your account password. Using the account credentials you set up through Azure will fail, and the same applies to any login you use for the SendGrid console.
  4. Port: TLS ports 587, 25 or 2525, or 465 if you need SSL.
  5. TLS: your SMTP client and its dependencies must support TLS 1.2 or higher, or the connection can fail with the 535 error. With TLS 1.0 or 1.1 the connection never reaches SendGrid's servers, so nothing is logged on their side.
  6. Whitespace: SMTP is line-oriented, and stray linefeed characters prevent authentication. This bites when you Base64-encode a key in a shell that wraps long output.
Host:     smtp.sendgrid.net
Port:     587
Username: apikey
Password: {your-full-api-key}

If you are using old username-and-password logins, SendGrid says its API endpoints reject new requests and SMTP configurations made with a username and password via Basic Authentication. Switch to a key. Our SMTP glossary entry covers the protocol basics.

6. Rule out an IP allow list, permissions and account status

If the key is right and the message differs slightly, match it against SendGrid's table:

  • The requestor's IP address is not whitelisted means IP Access Management is on and your server's IP is not in the allow list. An administrator must add the IP or turn the feature off. This often hits after moving hosts or changing a NAT address.
  • Authenticated user is not authorized points to a Teammate without the right permissions.
  • Account Disabled means the account was suspended, banned or deactivated by Consumer Trust. In most cases SendGrid emails you about it, so look for that message and reply. If no message was sent, for example after a suspension for dormancy, open a ticket with technical support.
  • Maximum credits exceeded means you reached your plan's credit limit; free trial accounts count more than 100 daily credits. The fix is to upgrade or wait for the next reset, which is daily for Free Trial direct accounts.

Read the exact message text

Several different 401 responses share a status code. The message string tells you whether to rotate the key, change an allow list or talk to support.

Checklist before you try again

  • The key exists under Settings > API Keys and has the scopes your endpoint needs.
  • The deployed app actually reads the new value (restart done, correct environment).
  • No quotes, spaces or line breaks around the key.
  • Web API: Authorization: Bearer {key}. SMTP: username apikey, password is the key.
  • The key belongs to the same account or subuser you are sending from.
  • Your server's IP is allowed if IP Access Management is on.
  • The scopes curl test above returns 200.

If nothing works

Contact SendGrid support through the SendGrid support center with the details below. For SMTP failures, SendGrid also has a dedicated 535 authentication troubleshooting article.

Include:

  • The full response body and HTTP status, or the full SMTP reply.
  • A UTC timestamp of one failed call.
  • The key's name and API Key ID (never the key itself).
  • The account or subuser name, and the sending server's public IP.
  • What you already tried (new key, header check, scopes test result).
Subject: 401 "The provided authorization grant is invalid, expired, or revoked"

Hi SendGrid support,

Since {date and time, UTC}, requests from {server IP} to {endpoint or smtp.sendgrid.net:587}
return: "The provided authorization grant is invalid, expired, or revoked".

Account/subuser: {name}
API key name and ID: {name} / {id} (created {date})
Already tried: created a new key with Mail Send access, confirmed Bearer header,
tested GET /v3/scopes (result: {status}), checked IP Access Management.

Please confirm whether the account is in good standing and why this key is rejected.

How to prevent it

  • Give each app its own named key, so deleting one never breaks another.
  • Rotate by creating the new key, deploying it, confirming sends work, then deleting the old one.
  • Keep keys out of source code and public repos, since exposed keys can be auto-deleted.
  • Alert on 401s from your sending service. Auth failures are silent otherwise; mail just stops.
  • Watch bounces and complaints, because a poor sender reputation is a common path to account review. This matters most for transactional email that users wait on.

Common questions

Is this an OAuth problem?

No. The wording matches the invalid_grant language from OAuth 2.0, but SendGrid uses API keys here. Treat it as "the key or account was not accepted."

Why did a key that worked yesterday stop?

SendGrid lists a deleted key, changed permissions and a key stored in an environment variable that suddenly stops working as causes. Also check for an exposed-key deletion notice, an IP allow list change and account status.

Can I recover a deleted API key?

No. SendGrid says it can't retrieve or restore a deleted or unknown key, so create a new one and update every app that used the old one.

Why does SMTP fail when my API key works over HTTP?

The usual culprits are the username (it must be apikey), line breaks in the password, or an SMTP client that does not support TLS 1.2. The Web API and SMTP use the same key but different client code paths.

strategysendgriderror-codes
Share this article
Sohail Hussain

Sohail Hussain

Founder & CEO at Mailneo

Building Mailneo — AI-powered email marketing for growing businesses.

Ready to supercharge your email marketing?

Start sending smarter emails with AI-powered campaigns. No credit card required.

Get Started Free