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'
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
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With