Objective
To export all phone numbers from a Twilio account into a CSV file using a Python script. This is useful for auditing, documentation, number inventory management, or migration purposes.
Product
Twilio Phone Numbers
User Account Permission/Role(s) Required
This process requires your Twilio Account SID and Auth Token, which are available in the Twilio Console. Keep these credentials confidential and never share them with anyone, including Twilio Support.
Roles that typically have access to these credentials:
- Account Owner
- Administrator
Procedure
Prerequisites
Before starting, make sure you have the following installed on your computer:
- Visual Studio Code (or any other code editor)
- Python 3.x
- Install the Twilio Python Library
Open a terminal (or the integrated terminal in VS Code) and run:
pip install twilio
- Create the Python Script
Create a new file called `export_numbers.py` and paste the following code:
import csv
from twilio.rest import Client
# Your Twilio credentials
# Find these at: https://console.twilio.com → Account Info
ACCOUNT_SID = "Paste your ACCOUNT_SID here" # Example: ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
AUTH_TOKEN = "Paste your AUTH_TOKEN here" # Example: 271xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# Initialize the Twilio client
client = Client(ACCOUNT_SID, AUTH_TOKEN)
# Fetch all phone numbers from the account
numbers = client.incoming_phone_numbers.list()
# Write the results to a CSV file
with open("twilio_phone_numbers.csv", mode="w", newline="") as file:
writer = csv.writer(file)
writer.writerow(["Phone Number", "Friendly Name", "SID"])
for number in numbers:
writer.writerow([number.phone_number, number.friendly_name, number.sid])
print("CSV file generated successfully: twilio_phone_numbers.csv")
- Add Your Twilio Credentials
In the script, replace the placeholder values with your actual credentials:
ACCOUNT_SID = "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" AUTH_TOKEN = "your_auth_token_here"
Note: Never commit your AUTH_TOKEN to version control (for example, Git) or share it with anyone. If you believe your Auth Token has been compromised, rotate it immediately: Auth Tokens and How to Change Them.
- Run the Script
In the terminal, navigate to the folder where you saved the script and run:
python export_numbers.py
Depending on the IDE you are using, you can simply press the Play button to run the script.
If successful, you will see the following message in the terminal:
CSV file generated successfully: twilio_phone_numbers.csvThe file twilio_phone_numbers.csv will be created in the same folder as the script. Open it in Excel, Google Sheets, or any spreadsheet application. It will contain the following columns:
| Phone Number | Friendly Name | SID |
|---|---|---|
| +12345678901 | (234) 567-8901 | PNxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
| +12345678901 | (234) 567-8901 | PNxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |Additional Information
- Large accounts: The incoming_phone_numbers.list() method retrieves all numbers by default. For accounts with a very large number of phone numbers, you can add a limit parameter to control how many records are fetched at once.
- Additional fields: The script exports Phone Number, Friendly Name, and SID by default. You can extend it to include additional fields such as the SMS or Voice URL, capabilities, and date created. All available fields are documented in the Incoming Phone Numbers resource reference.