Skip to contentSkip to navigationSkip to topbar
Page tools
Useful for sharing or LLM

On this page
Looking for more inspiration?Visit the

API Keys


(warning)

Twilio SendGrid API key limitations

  • Twilio supports a key longer than 69 characters. No exceptions can be made for third-party infrastructure.
  • Twilio limits accounts to 100 API keys.
  • You can't give an API key greater permissions than your own.
  • The Twilio SendGrid Console displays the API key only once at creation.
  • Treat your API key like a password and store it somewhere safe.
  • Twilio can't retrieve or restore an unknown or deleted API key.

To authenticate access to Twilio SendGrid services, use Application Programming Interface (API) keys.

They are the preferred alternative to using a username and password because you can revoke an API key at any time without having to change your username and password. We suggest that you use API keys for connecting to all of SendGrid's services.


Manage API keys

manage-api-keys page anchor

Go to Settings > API Keys(link takes you to an external page). The API Keys page displays your current API keys with the following parameters:

ParameterPurpose
NameThe name you defined for your API key.
API Key IDThe way you would reference your API key for management through the API.
ActionActions you can perform on your API keys, such as editing or deleting the key.

Create an API key

create-an-api-key page anchor

To use the Mail API, you need an API key.

  1. Go to Settings > API Keys(link takes you to an external page). The API Keys page appears.

  2. Click Create API Key. The Create API Key appears.

  3. Type a human-readable name for your API key in the API Key Name box.

  4. Choose the permissions for your API Key:

    API key permissionAccess details for account
    Full AccessGrants all API endpoints full access to your account.
    Custom AccessChoose both which API endpoints and which access levels each have to your account.
    Billing AccessGrants all billing API endpoints full access to your account.
    Email Address Validation AccessGrants read access to the reporting console and full access to validation API endpoints on your account.
  5. Click Create & View. The API Key Created page displays your created API Key.

  6. To copy the API key, click on it.

    • Store this key somewhere not in the public view.
    • Never include your API Key in your code or commit it to a public location or repository like GitHub.

To make billing-related API calls, Twilio requires a separate API key. By providing greater control over who has access to the responsibilities associated with your account, this segmentation improves security.

Store your API key in an environment variable

store-your-api-key-in-an-environment-variable page anchor

To further improve account security, store your API key in a local environment variable. This limits access to the API to individuals with the API key stored on their local environment. Supported programming languages can read environment variables for the API key values.

This section stores your key in a local environment variable named SENDGRID_API_KEY.

To store your Twilio SendGrid API key, open your command line and run the following command, replacing YOUR_API_KEY with the API key you created and saved in the previous section:

macOS TerminalWindows command linePowerShell
export SENDGRID_API_KEY=YOUR_API_KEY

To access the variable in your app, follow the convention from your programming language.

PythonNode.jsPHPC# or .NETJavaRubyGo

Read the API key in Python

api_key = os.environ.get('SENDGRID_API_KEY')

To change the human-readable name or the permission levels of an API key, follow this procedure:

  1. Go to Settings > API Keys(link takes you to an external page). The API Keys page appears.
  2. Click the action menu, then Edit API Key for the key you want to edit. The Edit API Key panel displays.
  3. From this panel, you can change the name or permission levels of your key.
  4. When finished making changes, click Update. The API Key page displays.

To change the human-readable name or the permission levels of an API key, follow this procedure:

  1. Go to Settings > API Keys(link takes you to an external page). The API Keys page appears.
  2. Click the action menu, then Delete API Key for the key you want to delete. The Delete API Key? modal displays.
  3. Click Confirm. The API Key page displays without the deleted API key.

Twilio SendGrid rejects any subsequent API calls made using this deleted API key.


To replace your API key, follow this procedure:

  1. Delete your API key.
  2. Create an API key.
  3. Replace the old API key with the new one in your code.

To test your created API key, use cURL:

Test an API key against the Mail Send endpoint

test-an-api-key-against-the-mail-send-endpoint page anchor
1
curl -i --request POST \
2
--url https://api.sendgrid.com/v3/mail/send \
3
--header 'Authorization: Bearer YOUR_API_KEY_HERE' \
4
--header 'Content-Type: application/json' \
5
--data '{
6
"personalizations": [
7
{
8
"to": [
9
{
10
"email": "recipient@example.com"
11
}
12
]
13
}
14
],
15
"from": {
16
"email": "sendeexampexample@example.com"
17
},
18
"subject": "Hello, World!",
19
"content": [
20
{
21
"type": "text/plain",
22
"value": "Howdy!"
23
}
24
]
25
}'

Look for a 202 Accepted in the HTTP response headers.