How to Send iMessages from Python in 5 Minutes
If you want to send iMessages from a Python script, bot, or backend service, you can do it in under 5 minutes with the Sendblue Python SDK. This tutorial walks you through installation, sending your first message, handling replies via webhooks, and sending media attachments.
Prerequisites
You need Python 3.7+ installed and a Sendblue account. Sign up at dashboard.sendblue.com to get your API key and secret — it's free, no credit card required.
You can also use the Sendblue CLI to create your account: npm install -g @sendblue/cli && sendblue setup
Install the Sendblue Python SDK
Install the SDK via pip:
pip install sendblueThat's it. The SDK handles authentication, request formatting, and error handling.
Send Your First iMessage
Create a Python file and add the following code:
from sendblue import Sendblue
# Initialize with your API credentials
sendblue = Sendblue(
api_key="your_api_key",
api_secret="your_api_secret"
)
# Send an iMessage
response = sendblue.send_message(
number="+15551234567",
content="Hello from Python! 🚀",
send_style="celebration" # optional iMessage effect
)
print(response)Run this and your recipient will receive a blue bubble iMessage with a celebration effect. If they don't have iMessage, Sendblue automatically falls back to RCS, then SMS.
The send_style parameter is optional and supports iMessage effects like celebration, fireworks, lasers, love, confetti, and more.
Send Media Attachments
Send images, videos, PDFs, or any file alongside your message:
# Send a message with an image
response = sendblue.send_message(
number="+15551234567",
content="Check out this product!",
media_url="https://example.com/product-image.jpg"
)
# Send a contact card (vCard) - unique to Sendblue
response = sendblue.send_message(
number="+15551234567",
media_url="https://example.com/contact.vcf"
)Contact cards (vCards) are powerful — when the recipient taps to save, your business name appears on all future messages. Sendblue is the only iMessage API that supports programmatic vCard delivery.
Check if a Number Has iMessage
Before sending, you can check whether a phone number supports iMessage:
# Evaluate if a number has iMessage
result = sendblue.evaluate_service("+15551234567")
print(result)
# Returns: {"is_imessage": true, "number": "+15551234567"}This is useful for routing: send iMessage to iPhone users and SMS to everyone else for maximum deliverability.
Receive Messages with Webhooks
Set up a webhook endpoint to receive replies. Here's a Flask example:
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route("/webhook", methods=["POST"])
def receive_message():
data = request.json
sender = data.get("from_number")
message = data.get("content")
media = data.get("media_url")
print(f"Message from {sender}: {message}")
# Process the message (AI, routing, logging, etc.)
# Then reply:
sendblue.send_message(
number=sender,
content="Thanks for your message!"
)
return jsonify({"status": "ok"}), 200
if __name__ == "__main__":
app.run(port=5000)Configure your webhook URL in the Sendblue dashboard under Settings → Webhooks. For local development, use ngrok to expose your local server.
Next Steps
You're now sending and receiving iMessages from Python. Here's what to explore next:
- Build an AI agent — Connect Claude or GPT-4 to your webhook for intelligent auto-replies
- Webhook deep dive — Handle delivery receipts, typing indicators, and media attachments
- Zapier integration — Connect iMessage to 7,000+ apps without code
- Full API docs — Explore group messaging, FaceTime Audio, carousel messages, and more
The Python SDK supports all Sendblue API features. Check the documentation for the complete reference.
Ready to send your first iMessage?
Get API access in minutes. Free sandbox, no credit card required.