Objective
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
Procedure
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:
Additional information
- 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