API Keys
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.
Go to Settings > API Keys. The API Keys page displays your current API keys with the following parameters:
| Parameter | Purpose |
|---|---|
| Name | The name you defined for your API key. |
| API Key ID | The way you would reference your API key for management through the API. |
| Action | Actions you can perform on your API keys, such as editing or deleting the key. |
To use the Mail API, you need an API key.
-
Go to Settings > API Keys. The API Keys page appears.
-
Click Create API Key. The Create API Key appears.
-
Type a human-readable name for your API key in the API Key Name box.
-
Choose the permissions for your API Key:
API key permission Access details for account Full Access Grants all API endpoints full access to your account. Custom Access Choose both which API endpoints and which access levels each have to your account. Billing Access Grants all billing API endpoints full access to your account. Email Address Validation Access Grants read access to the reporting console and full access to validation API endpoints on your account. View all access details for custom access -
Click Create & View. The API Key Created page displays your created API Key.
-
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.
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:
export SENDGRID_API_KEY=YOUR_API_KEY
To access the variable in your app, follow the convention from your programming language.
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:
- Go to Settings > API Keys. The API Keys page appears.
- Click the action menu, then Edit API Key for the key you want to edit. The Edit API Key panel displays.
- From this panel, you can change the name or permission levels of your key.
- 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:
- Go to Settings > API Keys. The API Keys page appears.
- Click the action menu, then Delete API Key for the key you want to delete. The Delete API Key? modal displays.
- 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:
- Delete your API key.
- Create an API key.
- Replace the old API key with the new one in your code.
To test your created API key, use cURL:
1curl -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.