SUPPORT.TWILIO.COM END OF LIFE NOTICE: This site,, is scheduled to go End of Life on February 27, 2024. All Twilio Support content has been migrated to, where you can continue to find helpful Support articles, API docs, and Twilio blog content, and escalate your issues to our Support team. We encourage you to update your bookmarks and begin using the new site today for all your Twilio Support needs.

Troubleshooting 401 - Unauthorised Error issue while using Sendgrid Email API in Twilio Functions

This guide is intended to help troubleshooting the 401 - Unauthorised error that commonly occurs when using the Sendgrid Email API in Twilio Functions. This guide covers the following topics for troubleshooting and resolving your issues:

  • Understanding 401 HTTP status code
  • Possible reason for the error
  • Steps to resolve the issue

Understanding 401 HTTP status code

The HTTP 401 - Unauthorised response status code indicates that the request that is made to the server is not completed successfully as it lacks the valid authentication credentials to access the requested resource from the server. This error commonly occurs on the client-side and it will not be logged in the server.

Possible reason for the error

This error occurs due to multiple reasons, for example, while trying to access a resource with incorrect or invalid credentials and when trying to access the resource which does not belong to our account.

In context of Sendgrid Email API and Functions, this error occurs due to the following reasons.

  • Failing to provide Twilio Auth Credentials
  • Using incorrect or invalid Sendgrid API key
  • Using non-verified FROM email address

Steps to resolve the issue

Resolving this issue is fairly simple and will not require a dedicated developer to fix the issue. Kindly following the given instructions below and verify the details that are given in it are correct.

1. Twilio Credentials:

As discussed earlier, the 401 error occurs when we generally provide an incorrect or invalid credentials that are not related to resource that we request. In general, Twilio Functions will access these credentials from its Environment. Kindly follow the instructions given in this article, which will guide you through verifying the same - Click here (Twilio article).

 2. Setting correct Sendgrid API Key in the Environment:

As the name suggests, the Sendgrid API Key is the key to unlock the access to the resources provided by the Sendgrid API. Without the API Key, it is not possible to make an authenticated request to access the Sendgrid API resource, which will eventually return the 401 error.

To avoid this situation, you must fetch the API Key from your Sendgrid account by referring to the instructions given in this documentation - Click here (Sendgrid Docs).
Note: The Sendgrid API Key will have the prefix 'SG'.

Once the API Key is generated, you must store it somewhere safely as it can given access to your Sendgrid account resources. You can now store this API Key in the Twilio Function environment with the variable named SENDGRID_API_KEY and by setting its value with the Sendgrid API key itself. To learn more about storing the API key in the environment variables, refer to this documentation - Click here (Sendgrid Docs).

3. Verify Sender Email Address:

Be advised that the Unauthorised error is not just about the credentials, but also the resource that we make request to. In general Sendgrid does not provide an Email address to send an Email. Whereas, we use our own Email addresses from different providers to send an Email.
However, Sendgrid will not have a direct access to use these Email addresses in order to send any Email or to make any changes to it. So, it will require to verify these Email addresses with Sendgrid platform in order to get necessary access. Otherwise, as we are not authorised to make any changes to the Email addresses provided by different providers, we will end up receiving the 401 - Unauthorised error.

 You can refer to the following documentation which will guide you through adding the Sender address and getting it verified in the Sendgrid platform - Click here (Sendgrid Docs).

Once all changes are made, the final configuration in the Environment variables of the Function environment should look like as shown in the image given below.


Note: The values given in the image is just for an example. Kindly ensure to include the actual details in there.

If you are still facing any issues after making these changes or if you will require any assistance, feel free to reach to: 

Twilio Support 

Sendgrid Support 

Additional resources

Learn more about Twilio Functions

Learn more about Sendgrid API Key

Learn more about Sendgrid Senders

Learn how to send Email with Sendgrid Email API and Twilio Functions

Quick Deploy app to forward incoming messages to email using Sendgrid Email API and Twilio functions


Have more questions? Submit a request
Powered by Zendesk