Reputation: 127
Is it possible that using the phone number I can get the sub account number that this phone number belongs to in Twilio?
I am using the following in python:
import os
from twilio.rest import Client
os.environ['account_sid'] = 'Master_account_SID'
os.environ['auth_token'] = 'Master_account_token'
account_sid = os.environ['account_sid']
auth_token = os.environ['auth_token']
client = Client(account_sid, auth_token)
incoming_phone_num = client.incoming_phone_numbers.list(phone_number='+1XXXXXXXXXX', limit=10)
print(incoming_phone_num.account_sid)
The above returns error "object has no attribute 'account_sid'"
Thanks.
Upvotes: 0
Views: 779
Reputation: 186
Following the same idea as Alan's answer but since you were writing python:
from twilio.rest import Client
client = Client()
phoneNumber = '+1xxxxxxxxxx'
subaccounts = client.api.accounts.list(status='active', limit=1000)
for subaccount in subaccounts:
subclient = Client(account_sid=subaccount.sid)
numbers = subclient.incoming_phone_numbers.list(phone_number=phoneNumber, limit=1)
if len(numbers) > 0:
print(subaccount.sid, subaccount.friendly_name)
The key takeaway is that the phone numbers are only available in each subaccount so you need to loop through them to find the number you want.
Note that Twilio's python API will take credentials from environment variables itself so you didn't need to do that.
Upvotes: 0
Reputation: 10771
Here is JavaScript but you will get the general idea from the explanation below.
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);
const phoneNumberToSearch = '+1484xxxxxx';
let subAccount;
client.api.accounts.list({status: 'active', limit: 1000})
.then(accounts => {
accounts.forEach(a => {
subAccount = require('twilio')(accountSid, authToken, { accountSid: a.sid });
subAccount.incomingPhoneNumbers
.list({phoneNumber: phoneNumberToSearch, limit: 1})
.then(incomingPhoneNumbers => {
incomingPhoneNumbers.forEach(i => console.log(i.accountSid))
});
})
});
API being used:
Making a phone call with a Subaccount
Upvotes: 1