Reputation: 153
I'm trying to send a message using twilio and I get the error below. How do I resolve this error?
import os
from twilio.rest import Client
account_sid = os.environ['testtest']
auth_token = os.environ['testtesttest']
client = Client(account_sid, auth_token)
message = client.messages \
.create(
body="Join Earth's mightiest heroes. Like Kevin Bacon.",
from_='+16813203595',
to='+12345678'
)
print(message.sid)
raise KeyError(key) from None
KeyError: 'testtest'
Upvotes: 8
Views: 7926
Reputation: 11
Just assign value as you would do to any other dict!
account_sid=os.environ['TWILIO_ACCOUNT_SID']='value of sid'
auth_token=os.environ['TWILIO_AUTH_TOKEN']='value of auth token'
Upvotes: 1
Reputation: 487
For anyone in the future if you want the simple answer to this question.
In Twilio's sample code it has these two lines
account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
You don't actually place your Twilio account sid and token in these lines. KEEP THEM AS THEY ARE. Do not change these lines. Do not add your account sid and your token. Instead at the command prompt type in:
export TWILIO_ACCOUNT_SID=your_SID_account
export TWILIO_AUTH_TOKEN=your_auth_token
Now run the script and the script will pull your environmental keys. Keeps your info safe this way :)
Upvotes: 6
Reputation: 89
To easy test your code: A simple fix would be to not use environment variables yet, Restructure your code to:
from twilio.rest import Client
account_sid = 'testtest'
auth_token = 'testtesttest'
client = Client(account_sid, auth_token)
message = client.messages \
.create(
body="Join Earth's mightiest heroes. Like Kevin Bacon.",
from_='+16813203595',
to='+12345678'
)
print(message.sid)
Once pleased then consider and you get a response from twilio you can switch to env variables
Upvotes: 7
Reputation: 2696
Setting up environment variables is operating system dependent.
Here is a list of the links depending on the machine you are using
You will also need to set up your twilio account and have both your account_sid
and auth_token
from them
Upvotes: 3