yeroduk
yeroduk

Reputation: 153

twilio: raise KeyError(key) from None

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

Answers (4)

Mohammad Ali
Mohammad Ali

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

wildernessfamily
wildernessfamily

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

Cryton Andrew
Cryton Andrew

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

Paul Brennan
Paul Brennan

Reputation: 2696

Setting up environment variables is operating system dependent.

Here is a list of the links depending on the machine you are using

  1. Windows 10 https://superuser.com/questions/949560/how-do-i-set-system-environment-variables-in-windows-10
  2. Linux https://linuxize.com/post/how-to-set-and-list-environment-variables-in-linux/
  3. Mac https://apple.stackexchange.com/questions/106778/how-do-i-set-environment-variables-on-os-x

You will also need to set up your twilio account and have both your account_sid and auth_token from them

Upvotes: 3

Related Questions