This tutorial shows how to record outbound and inbound phone calls with Twilio in Python and retrieve automatic transcriptions of the recordings after calls complete. It targets developers building call logging systems, quality assurance tools, or transcript-based analytics pipelines. You will enable recording on a call, set up a recording status callback webhook, and fetch the transcription text once Twilio finishes processing the audio.
Prerequisites and Setup
You need Python 3.7 or higher, Flask, and the Twilio SDK installed with pip install twilio flask python-dotenv. Twilio records calls server-side, so no special setup is needed beyond enabling the record parameter; the audio is stored on Twilio's servers until you delete it or it expires. Set up a recording_status_callback URL pointing to a Flask webhook route so Twilio notifies your server when the recording is ready, which typically happens within a minute of the call ending. Transcriptions are created automatically when you set transcribe=True on the RecordingStatusCallback or by calling the Transcriptions API separately.
pip install twilio flask python-dotenv
# .env
TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TWILIO_AUTH_TOKEN=your_auth_token_here
TWILIO_PHONE_NUMBER=+15551234567
The Complete Code
Pass record=True to client.calls.create() to enable recording, and include a recording_status_callback URL so your server is notified when the recording file is available. The recording callback webhook receives a RecordingSid, RecordingUrl, and RecordingDuration in the POST body. After the recording is ready, fetch the transcription using client.recordings(recording_sid).transcriptions.list() and read the transcription_text field from the first result. Transcription can take 1 to 5 minutes after the recording completes depending on call duration.
import os
import time
from flask import Flask, request
from twilio.rest import Client
from twilio.twiml.voice_response import VoiceResponse
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
client = Client(os.environ["TWILIO_ACCOUNT_SID"], os.environ["TWILIO_AUTH_TOKEN"])
@app.route("/voice", methods=["POST"])
def voice():
resp = VoiceResponse()
resp.say("This call is being recorded for quality assurance. Please leave your message after the tone.", voice="alice")
resp.record(timeout=10, transcribe=True, transcribe_callback="/transcription")
resp.say("Thank you. Goodbye.", voice="alice")
return str(resp), 200, {"Content-Type": "text/xml"}
@app.route("/recording-status", methods=["POST"])
def recording_status():
recording_sid = request.form.get("RecordingSid", "")
recording_url = request.form.get("RecordingUrl", "")
duration = request.form.get("RecordingDuration", "")
print(f"Recording ready: {recording_sid} ({duration}s) at {recording_url}")
return "", 204
@app.route("/transcription", methods=["POST"])
def transcription():
recording_sid = request.form.get("RecordingSid", "")
transcription_text = request.form.get("TranscriptionText", "")
transcription_sid = request.form.get("TranscriptionSid", "")
print(f"Transcription {transcription_sid}: {transcription_text}")
return "", 204
def make_recorded_call(to: str, twiml_url: str, callback_url: str) -> str:
call = client.calls.create(
to=to,
from_=os.environ["TWILIO_PHONE_NUMBER"],
url=twiml_url,
record=True,
recording_status_callback=callback_url,
)
print(f"Call SID: {call.sid}")
return call.sid
if __name__ == "__main__":
app.run(port=5000, debug=True)
Testing and Debugging
Make a test call to your Twilio number, speak for 10 to 15 seconds, and then hang up; Twilio should POST to your recording status callback within 30 to 60 seconds of the call ending. Check the Flask terminal for the recording SID and URL to confirm the webhook is receiving the callback correctly. If no recording callback arrives, verify your ngrok tunnel is running and the callback URL is set correctly in both your calls.create() call and the TwiML Record verb. Access recordings directly from the Twilio Console under Monitor, Recordings if you need to verify the audio file exists before debugging the webhook.
Taking It Further
Download the recording MP3 by appending .mp3 to the RecordingUrl and store it in S3 for long-term retention beyond Twilio's default recording storage period. Use the transcription text to run keyword search, sentiment analysis, or PII detection using standard Python NLP libraries. Build a compliance system that flags calls containing specific keywords and routes them to a quality assurance queue for human review. Automatically delete recordings from Twilio after downloading them to S3 to reduce storage costs by calling client.recordings(sid).delete().
Talk to Telphi Consulting about building a compliant call recording and transcription pipeline for your contact center.
Ready to Transform Your Business Communications?
Get a free consultation with our VoIP experts and discover how we can help you save costs, improve efficiency, and scale your business.
Comments (0)
Join the discussion and share your thoughts (AI-moderated for quality)
Be the first to comment
No comments yet. Share your thoughts below.