Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

twilio: raise KeyError(key) from None

Tags:

python

twilio

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'

like image 460
yeroduk Avatar asked Nov 23 '20 03:11

yeroduk


2 Answers

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

like image 81
Cryton Andrew Avatar answered Nov 15 '22 12:11

Cryton Andrew


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 :)

like image 45
wildernessfamily Avatar answered Nov 15 '22 10:11

wildernessfamily