I am using a Twilio account for sending SMS to users. I have installed the SDK by executing the following command.
composer require twilio/sdk
I’m using the following code snippet to send a message.
public function sendSms($data)
{
// Your Account SID and Auth Token from twilio.com/console
$account_sid = 'sid';
$auth_token = 'token';
$twilio_number = "number";
$client = new Client($account_sid, $auth_token);
return $client->messages->create(
$data['phone'], array(
'from' => $twilio_number,
'body' => $data['message'],
)
);
}
According to Twilio documentation, this code should send the message with my valid credentials, but I am getting the following error.
[HTTP 401] Unable to create record:
Authenticate85/var/www/art/api/vendor/twilio/sdk/Twilio/Version.php
I have researched this issue, but I got no guidance; any clues? Note: I have a balance in my Twilio account.
3
Answers
As @dwhitz mentions,
HTTP 401
is an Unauthorized Error (aka no authorization).If you run into the same error, that likely means your credentials aren’t set up correctly.
Step 1: Locate Credentials
All requests to Twilio’s REST API need to be authenticated. Twilio supports two forms of authentication (both using HTTP basic auth), so you can pick either option:
1. Account SID & Account Token
These can be found on your Account Dashboard on the Twilio Console.
2. API Keys
You can create an API Key within Settings->API Keys on your Twilio console.
Step 2: Secure Credentials
You should now have either an (Account SID + Account Token) or an (API Key SID + API Key Secret). These pieces of information should be hidden and not publicly available (don’t share them with your co-workers or push them to GitHub).
Secure them by storing them in a way that prevents unauthorized access, like environment variables. Here are examples using the 1st form (Account SID & Account Token).
Via Mac/Linux
Make sure to add twilio.env to your .gitignore
Via Windows
Step 3: Load Your Credentials
If you’re getting the
HTTP 401
error, this is likely the biggest point of failure. You’ll want to make sure you’re loading the credentials properly.Now, this depends on your language, but since this question is in PHP I’ll answer it in that language using
getenv()
.After this, you’ll initialize your Twilio Client like so:
PS. There are other ways to work with environment variables in PHP, so check out this blog post if you’d like to do it another way.
Step 4: Still Not Working?
At this point, if you’re still getting
HTTP 401
there’s likely something wrong with how you’re storing/reading the credentials via environment variables.To debug, you could add print/echo statements to output the data of your env variables. If no data is present, you might want to do more research on working with environment variables.
I suffered days with this, but believe me it’s black magic!
How does the error happen: you copy the token from twilio and it is automatically modified in your clipboard. It also happens so sublime and in notepad.
Solution: on the twilio website enter the browser’s debug mode and copy the token through the html div element, so it will be copied correctly to your computer’s clipboard.
Someone explain this I don’t understand witchcraft.
In my own case, I figured out my Twilo credit had exhausted, and so automatically Twilo had suspended my account. I recharged my Twilo account and everything got normal.