Search code examples
python-3.xtwiliotwilio-conversations

Twilio Conversations API - unable to fetch record


I am trying to fetch a conversation following Twilio documentation verbatim here.

import os
from twilio.rest import Client


# Find your Account SID and Auth Token at twilio.com/console
# and set the environment variables. See http://twil.io/secure
account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
client = Client(account_sid, auth_token)

conversation = client.conversations \
                     .conversations('CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \
                     .fetch()

This returns a :

HTTP 404 error: Unable to fetch record: The requested resource /Conversations/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX was not found

However if I supply the conversation service id like so:

import os
from twilio.rest import Client


# Find your Account SID and Auth Token at twilio.com/console
# and set the environment variables. See http://twil.io/secure
account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
client = Client(account_sid, auth_token)

conversation = client.conversations \
                     .services("ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
                     .conversations('CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \
                     .fetch()

I get the conversation object correctly. In my case I am using webhooks to catch when a participant joins a conversation, since the event doesn't return the service sid, only the conversation sid, it complicates what I am trying to do. What am I missing?


Solution

  • Twilio developer evangelist here.

    The short form conversations API, like this:

    conversation = client.conversations \
                         .conversations('CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \
                         .fetch()
    

    only works if you are using the default service. Otherwise you do need to provide the service SID.

    If I were you I would store the conversations service SID as a config parameter in your application, perhaps set as an environment variable, so that you can refer to it when making requests to the API like this.