{"id":18081,"date":"2026-11-18T23:46:38","date_gmt":"2026-11-18T18:16:38","guid":{"rendered":"https:\/\/www.fast2sms.com\/help\/python-sms-api\/"},"modified":"2026-11-19T00:52:15","modified_gmt":"2026-11-18T19:22:15","slug":"python-sms-api","status":"publish","type":"post","link":"https:\/\/www.fast2sms.com\/help\/python-sms-api\/","title":{"rendered":"Send SMS and OTP in Python with Fast2SMS API (requests)"},"content":{"rendered":"<p>You can send SMS and OTP from Python with the Fast2SMS API using the <code>requests<\/code> library and a few lines of code. This guide covers a DLT SMS from your own sender ID, a login OTP with <a href=\"\/help\/smart-otp-setup-guide-fast2sms\/\">Smart OTP<\/a> (send, verify and resend), delivery reports by API and by webhook, and error handling. The last section has a complete Python module you can drop into a Flask, Django, FastAPI or plain script project.<\/p>\n<aside class=\"f2s-cta\" aria-label=\"Get started with Fast2SMS\"><div class=\"f2s-cta__copy\"><strong class=\"f2s-cta__title\">Send SMS from Python<\/strong><p>Copy your API key, add your DLT or Smart OTP template and send your first message from Python today.<\/p><\/div><div class=\"f2s-cta__actions\"><a class=\"f2s-cta__btn f2s-cta__btn--primary\" href=\"https:\/\/www.fast2sms.com\/\"><span>Sign up free<\/span> <span class=\"f2sh-btn__arrow\" aria-hidden=\"true\">&rarr;<\/span><\/a><a class=\"f2s-cta__btn f2s-cta__btn--ghost\" href=\"https:\/\/www.fast2sms.com\/meet\/\" target=\"_blank\" rel=\"noopener\">Book a free meeting<\/a><\/div><\/aside>\n<h2>Before you start<\/h2>\n<ul>\n<li><strong>Python 3.8 or later<\/strong> and the <code>requests<\/code> package.<\/li>\n<li><strong>API key<\/strong> from <strong>API Key<\/strong> under UTILITY, with <strong>API Access<\/strong> on.<\/li>\n<li><strong>DLT SMS:<\/strong> an approved sender ID and content template in <strong>DLT Manager<\/strong>. Note the <strong>Message ID<\/strong> of the template.<\/li>\n<li><strong>OTP:<\/strong> a Smart OTP template in <strong>OTP Template List<\/strong>. Note its <strong>OTP ID<\/strong>.<\/li>\n<li><strong>Wallet balance<\/strong> for what you send. Each SMS, OTP send and OTP resend is charged.<\/li>\n<li><strong>Run it on your server.<\/strong> Never ship the <a href=\"\/help\/api-key-find-reset\/\">API key<\/a> inside a mobile app, desktop app or browser code.<\/li>\n<\/ul>\n<h2>Find the values you need in the panel<\/h2>\n<ol class=\"f2s-steps\">\n<li>\n<h3>API key<\/h3>\n<p>Go to <strong>Account Setting<\/strong>, <strong>API Key<\/strong>. Click <strong>View<\/strong>, enter the OTP that comes to your account mobile and copy the <strong>Authorization Key<\/strong>.<\/p>\n<figure class=\"f2s-shot\"><img src=\"https:\/\/www.fast2sms.com\/help\/wp-content\/uploads\/2026\/11\/api-key-hd-13-scaled.webp\" width=\"1440\" height=\"900\" loading=\"lazy\" decoding=\"async\" alt=\"API Key page showing a masked Authorization Key with View and Reset buttons and the API Access switch on\"><figcaption>Keep API Access on, or every request gets error 413.<\/figcaption><\/figure>\n<\/li>\n<li>\n<h3>Message ID and sender ID for DLT SMS<\/h3>\n<p>In the SMS app open <strong>DLT Manager<\/strong>, <strong>Content Template<\/strong>. Copy the <strong>Message ID<\/strong> and the <strong>Sender ID<\/strong> from the card of the approved template you want to send.<\/p>\n<figure class=\"f2s-shot\"><img src=\"https:\/\/www.fast2sms.com\/help\/wp-content\/uploads\/2026\/11\/sms-dlt-template-list-hd-15-scaled.webp\" width=\"1440\" height=\"900\" loading=\"lazy\" decoding=\"async\" alt=\"DLT Manager Content Template cards with Message ID, Sender ID, Template ID and Entity and the Sample Code link\"><figcaption>The Message ID is the short number on the card.<\/figcaption><\/figure>\n<\/li>\n<li>\n<h3>OTP ID for Smart OTP<\/h3>\n<p>Open Smart OTP, <strong>OTP Template List<\/strong>, and copy the <strong>OTP ID<\/strong> of the template you want to use for login.<\/p>\n<figure class=\"f2s-shot\"><img src=\"https:\/\/www.fast2sms.com\/help\/wp-content\/uploads\/2026\/11\/smart-otp-template-list-hd-10-scaled.webp\" width=\"1440\" height=\"900\" loading=\"lazy\" decoding=\"async\" alt=\"Smart OTP Template List with OTP template cards for SMS and WhatsApp, each with its OTP ID\"><figcaption>Each Smart OTP template has its own OTP ID.<\/figcaption><\/figure>\n<\/li>\n<\/ol>\n<h2>Install and set up<\/h2>\n<pre><code class=\"language-bash\">pip install requests\nexport FAST2SMS_API_KEY=\"YOUR_API_KEY\"     # on Windows: set FAST2SMS_API_KEY=YOUR_API_KEY<\/code><\/pre>\n<p>Keep the key in an environment variable or your framework&#8217;s settings, never in Git. A shared helper makes every call the same:<\/p>\n<pre><code class=\"language-python\">import os\nimport requests\n\nBASE_URL = \"https:\/\/www.fast2sms.com\"\nAPI_KEY = os.environ[\"FAST2SMS_API_KEY\"]\n\nsession = requests.Session()\nsession.headers.update({\"authorization\": API_KEY, \"Accept\": \"application\/json\"})\n\n\ndef post(path, payload):\n    r = session.post(BASE_URL + path, json=payload, timeout=(5, 15))\n    try:\n        data = r.json()\n    except ValueError:\n        return False, r.status_code, {\"message\": \"Non-JSON response\"}\n    ok = r.ok and data.get(\"return\") is True\n    return ok, r.status_code, data<\/code><\/pre>\n<p><code>timeout=(5, 15)<\/code> means 5 seconds to connect and 15 seconds to read. Without a timeout, a slow network can block your web worker.<\/p>\n<div class=\"f2s-note\">Send the bare key in the <code>authorization<\/code> header. Do not add &#8220;Bearer &#8220;. The OTP endpoints do not remove it and reply &#8220;Invalid authorization key&#8221;.<\/div>\n<h2>Send a DLT SMS in Python<\/h2>\n<p>DLT SMS goes to <code>\/dev\/bulkV2<\/code> with <code>route=dlt<\/code>. You pass the template&#8217;s Message ID and the values for its variables, and Fast2SMS builds the approved text.<\/p>\n<p>The example uses Message ID 10001 on sender DEMOTR, with four variables: name, order number, amount and date.<\/p>\n<pre><code class=\"language-python\">ok, http, data = post(\"\/dev\/bulkV2\", {\n    \"route\": \"dlt\",\n    \"sender_id\": \"DEMOTR\",            # exact case from DLT Manager\n    \"message\": \"10001\",               # Message ID, not the 19-digit Template ID\n    \"variables_values\": \"|\".join([\"Rahul\", \"DT10458\", \"1240\", \"21 Nov\"]),\n    \"numbers\": \",\".join([\"9999999999\", \"8888888888\"]),\n    \"udf1\": \"order-DT10458\",          # optional reference, returned in webhooks\n})\n\nif ok:\n    print(\"Queued, request ID:\", data[\"request_id\"])   # S...\nelse:\n    print(\"Not sent:\", data.get(\"status_code\", http), data.get(\"message\"))<\/code><\/pre>\n<ul>\n<li>Values follow the order of <code>{#var#}<\/code> in the template. Each value is cut to 40 characters.<\/li>\n<li>The sender ID must match exactly, including upper and lower case.<\/li>\n<li>On success <code>data[\"message\"]<\/code> is a list, <code>[\"SMS sent successfully.\"]<\/code>. That means queued, not delivered.<\/li>\n<li>Add <code>\"schedule_time\": \"25-11-2026-10-30\"<\/code> (DD-MM-YYYY-HH-MM in IST) to send later.<\/li>\n<li>Add <code>\"sms_details\": 1<\/code> to get the character count, SMS parts and amount in the reply.<\/li>\n<\/ul>\n<h2>Send and verify an OTP with Smart OTP<\/h2>\n<p>With Smart OTP, Fast2SMS makes the code, sends it and checks it. Your Python app only passes the mobile number and later the code the user typed.<\/p>\n<h3>Send<\/h3>\n<pre><code class=\"language-python\">ok, http, data = post(\"\/dev\/otp\/send\", {\n    \"mobile\": \"9999999999\",\n    \"otp_id\": \"YOUR_OTP_ID\",\n    \"otp_expiry\": 5,      # minutes (1 to 10080), default 15\n    \"otp_length\": 6,      # 4 to 10, default 6\n})\nprint(\"OTP sent\" if ok else data.get(\"message\"))<\/code><\/pre>\n<h3>Verify<\/h3>\n<pre><code class=\"language-python\">def verify_otp(mobile, otp):\n    ok, http, data = post(\"\/dev\/otp\/verify\", {\"mobile\": mobile, \"otp\": otp})\n    return ok, data.get(\"message\", \"\")\n\nok, msg = verify_otp(\"9999999999\", \"482913\")\nif ok:\n    print(\"Verified, log the user in\")\nelse:\n    print(\"Not verified:\", msg)   # Invalid OTP \/ OTP expired \/ OTP not found or already verified<\/code><\/pre>\n<p>No OTP ID is needed to verify. Fast2SMS checks the latest unverified OTP for that mobile. A new send to the same number cancels all older OTPs, so do not send again on page refresh.<\/p>\n<h3>Resend<\/h3>\n<pre><code class=\"language-python\">ok, http, data = post(\"\/dev\/otp\/resend\", {\"mobile\": \"9999999999\"})\nprint(\"Sent again\" if ok else data.get(\"message\"))<\/code><\/pre>\n<p>Resend sends the <strong>same<\/strong> OTP and is charged again. It is allowed within 10 minutes of the first send and up to 5 sends in total, so the user gets 4 resends. Between sends there is a wait set in <strong>OTP Settings<\/strong> (default 10 seconds); a request inside the wait gets &#8220;Please wait N seconds before requesting a new OTP&#8221;.<\/p>\n<h2>Get the delivery status<\/h2>\n<p>A success reply only means the message was accepted. Use either the DLR API or a webhook to learn the final status. Do not use both for the same messages, because reading a DLR by API can stop the webhook for those rows.<\/p>\n<h3>DLR API<\/h3>\n<pre><code class=\"language-python\">def delivery_report(request_id):\n    r = session.get(f\"{BASE_URL}\/dev\/dlr\/{request_id}\", timeout=(5, 15))\n    data = r.json()\n    if not data.get(\"success\"):\n        return []\n    return [(row[\"mobile\"], row[\"status\"]) for row in data[\"data\"][0][\"delivery_status\"]]\n\nfor mobile, status in delivery_report(\"Sa1B2c3D4e5F6g7\"):\n    print(mobile, status)     # Delivered, Failed or Sent (report pending)<\/code><\/pre>\n<p>The DLR API looks back 7 days. A 404 with &#8220;No DLR found for the given request_id&#8230;&#8221; means a wrong ID, an old ID, or only invalid numbers.<\/p>\n<h3>Webhook receiver (Flask)<\/h3>\n<p>Create the webhook in the panel: <strong>Webhook<\/strong>, then <strong>SMS<\/strong> or <strong>OTP<\/strong>, <strong>Add Webhook<\/strong>, format <strong>Standard Webhook<\/strong>, method POST, content type JSON and your public URL.<\/p>\n<figure class=\"f2s-shot\"><img src=\"https:\/\/www.fast2sms.com\/help\/wp-content\/uploads\/2026\/11\/webhook-create-modal-hd-3-scaled.webp\" width=\"1440\" height=\"900\" loading=\"lazy\" decoding=\"async\" alt=\"CREATE WEBHOOK (SMS) form with name, Standard Webhook format, event, Entity ID, method POST, URL, JSON content type and the JSON body with request_id, mobile and status fields\"><figcaption>The body on the right lists the fields your Python code will receive.<\/figcaption><\/figure>\n<pre><code class=\"language-python\">import os\nfrom flask import Flask, request, abort\n\napp = Flask(__name__)\n\n@app.post(\"\/fast2sms\/webhook\")\ndef fast2sms_webhook():\n    # Optional: a custom header you added in the webhook's Headers tab\n    if request.headers.get(\"X-Webhook-Token\") &#33;= os.environ.get(\"MY_WEBHOOK_TOKEN\"):\n        abort(401)\n\n    event = request.get_json(silent=True) or {}\n    request_id = event.get(\"request_id\")\n    mobile = event.get(\"mobile\")\n    status = event.get(\"status\")              # delivered or failed\n    reason = event.get(\"status_description\")\n    # save_status(request_id, mobile, status, reason)\n    return \"OK\", 200<\/code><\/pre>\n<p>Reply within 10 seconds with any 2xx code. On a timeout, HTTP 408, 429 or 5xx, Fast2SMS tries again up to 3 times, 60 seconds apart. Check every attempt in <strong>Webhook Logs<\/strong>. Webhooks are sent only for messages sent through the API, and only for reports that arrive within 24 hours.<\/p>\n<p>In Django, the same view is a function decorated with <code>@csrf_exempt<\/code> and <code>@require_POST<\/code> that reads <code>json.loads(request.body)<\/code>.<\/p>\n<aside class=\"f2s-cta\" aria-label=\"Get started with Fast2SMS\"><div class=\"f2s-cta__copy\"><strong class=\"f2s-cta__title\">Get delivery status in your Python app<\/strong><p>Add one webhook and Fast2SMS posts delivered or failed status for every API message to your URL.<\/p><\/div><div class=\"f2s-cta__actions\"><a class=\"f2s-cta__btn f2s-cta__btn--primary\" href=\"https:\/\/www.fast2sms.com\/\"><span>Sign up free<\/span> <span class=\"f2sh-btn__arrow\" aria-hidden=\"true\">&rarr;<\/span><\/a><a class=\"f2s-cta__btn f2s-cta__btn--ghost\" href=\"https:\/\/www.fast2sms.com\/meet\/\" target=\"_blank\" rel=\"noopener\">Book a free meeting<\/a><\/div><\/aside>\n<h2>Error handling<\/h2>\n<ul>\n<li><strong>Always check <code>return<\/code>.<\/strong> The wallet check can reply with HTTP 200 and <code>\"return\": false<\/code> with status_code 416. Do not trust <code>r.ok<\/code> alone.<\/li>\n<li><strong>Do not wrap sends in an automatic retry.<\/strong> Libraries like <code>urllib3.Retry<\/code> or <code>tenacity<\/code> can resend after a read timeout, which sends and charges twice. Retry only if the connection never opened. Verify is free and can be retried once.<\/li>\n<li><strong>Catch network errors<\/strong> with <code>requests.exceptions.ConnectionError<\/code> and <code>requests.exceptions.Timeout<\/code>.<\/li>\n<li><strong>Use API Logs<\/strong> in the panel to see the exact request and reply of the last 7 days.<\/li>\n<\/ul>\n<div class=\"f2s-facts\">\n<table>\n<thead>\n<tr>\n<th>Reply<\/th>\n<th>Meaning<\/th>\n<th>Fix<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>412 Invalid Authentication, Check Authorization Key<\/td>\n<td>Key wrong, old or missing (SMS endpoints)<\/td>\n<td>Copy the key again from <strong>API Key<\/strong>.<\/td>\n<\/tr>\n<tr>\n<td>Invalid authorization key<\/td>\n<td>Key wrong or has &#8220;Bearer &#8221; (OTP endpoints)<\/td>\n<td>Send the bare key.<\/td>\n<\/tr>\n<tr>\n<td>413 Authorization Key Disabled<\/td>\n<td>API Access is off<\/td>\n<td>Turn on <strong>API Access<\/strong>.<\/td>\n<\/tr>\n<tr>\n<td>414 IP is blacklisted from Dev API section<\/td>\n<td><a href=\"\/help\/api-security-ip-whitelist\/\">API Security<\/a> blocks your server IP<\/td>\n<td>Whitelist the IP in <strong>API Security<\/strong>.<\/td>\n<\/tr>\n<tr>\n<td>406 Invalid Sender ID<\/td>\n<td>Sender not yours or wrong case<\/td>\n<td>Copy it from <a href=\"\/help\/dlt-manager-entity-sender-template-guide\/\">DLT Manager<\/a>.<\/td>\n<\/tr>\n<tr>\n<td>424 Invalid Message ID<\/td>\n<td>Wrong ID, or template not linked to the sender<\/td>\n<td>Use the Message ID from the same card as the sender.<\/td>\n<\/tr>\n<tr>\n<td>411 Invalid Numbers<\/td>\n<td>No valid number in the list<\/td>\n<td>Send 10-digit mobiles starting with 6 to 9.<\/td>\n<\/tr>\n<tr>\n<td>416 You don&#8217;t have sufficient wallet balance<\/td>\n<td>Low balance<\/td>\n<td>Recharge.<\/td>\n<\/tr>\n<tr>\n<td>Invalid OTP ID<\/td>\n<td>OTP ID wrong, deleted or from another account<\/td>\n<td>Copy it from <strong>OTP Template List<\/strong>.<\/td>\n<\/tr>\n<tr>\n<td>Please wait N seconds before requesting a new OTP<\/td>\n<td>Resend wait<\/td>\n<td>Show a countdown in your app.<\/td>\n<\/tr>\n<tr>\n<td>OTP expired \/ Invalid OTP<\/td>\n<td>Too late, or wrong code<\/td>\n<td>Ask for a new OTP, or let the user retry.<\/td>\n<\/tr>\n<tr>\n<td>Maximum verification attempts reached<\/td>\n<td>Too many wrong codes<\/td>\n<td>Send a new OTP.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2>Full copy-paste example: fast2sms.py<\/h2>\n<pre><code class=\"language-python\">\"\"\"fast2sms.py - small Fast2SMS client for DLT SMS and Smart OTP.\"\"\"\nimport os\nimport requests\n\n\nclass Fast2SMSError(Exception):\n    pass\n\n\nclass Fast2SMS:\n    def __init__(self, api_key=None, base_url=\"https:\/\/www.fast2sms.com\"):\n        self.base_url = base_url\n        self.session = requests.Session()\n        self.session.headers.update({\n            \"authorization\": api_key or os.environ[\"FAST2SMS_API_KEY\"],\n            \"Accept\": \"application\/json\",\n        })\n\n    def _call(self, method, path, payload=None):\n        try:\n            r = self.session.request(method, self.base_url + path, json=payload, timeout=(5, 15))\n        except requests.exceptions.RequestException as exc:\n            raise Fast2SMSError(f\"Network error: {exc}\") from exc\n        try:\n            data = r.json()\n        except ValueError:\n            return {\"ok\": False, \"http\": r.status_code, \"message\": \"Non-JSON response\", \"raw\": r.text}\n        ok = r.ok and (data.get(\"return\") is True or data.get(\"success\") is True)\n        msg = data.get(\"message\", \"\")\n        if isinstance(msg, list):\n            msg = \" \".join(msg)\n        return {\n            \"ok\": ok,\n            \"http\": r.status_code,\n            \"status_code\": data.get(\"status_code\"),\n            \"request_id\": data.get(\"request_id\"),\n            \"message\": msg,\n            \"raw\": data,\n        }\n\n    def send_dlt(self, sender_id, message_id, variables, numbers, **extra):\n        payload = {\n            \"route\": \"dlt\",\n            \"sender_id\": sender_id,\n            \"message\": str(message_id),\n            \"variables_values\": \"|\".join(str(v) for v in variables),\n            \"numbers\": \",\".join(numbers),\n        }\n        payload.update(extra)          # schedule_time, sms_details, udf1...\n        return self._call(\"POST\", \"\/dev\/bulkV2\", payload)\n\n    def send_otp(self, mobile, otp_id, expiry=5, length=6, variables=None):\n        payload = {\"mobile\": mobile, \"otp_id\": otp_id, \"otp_expiry\": expiry, \"otp_length\": length}\n        if variables:\n            payload[\"variables_values\"] = \"|\".join(variables)\n        return self._call(\"POST\", \"\/dev\/otp\/send\", payload)\n\n    def verify_otp(self, mobile, otp):\n        return self._call(\"POST\", \"\/dev\/otp\/verify\", {\"mobile\": mobile, \"otp\": otp})\n\n    def resend_otp(self, mobile):\n        return self._call(\"POST\", \"\/dev\/otp\/resend\", {\"mobile\": mobile})\n\n    def delivery_report(self, request_id):\n        return self._call(\"GET\", f\"\/dev\/dlr\/{request_id}\")\n\n\nif __name__ == \"__main__\":\n    f2s = Fast2SMS()\n\n    res = f2s.send_dlt(\"DEMOTR\", 10001, [\"Rahul\", \"DT10458\", \"1240\", \"21 Nov\"], [\"9999999999\"])\n    print(\"SMS:\", res[\"ok\"], res[\"request_id\"] or res[\"message\"])\n\n    res = f2s.send_otp(\"9999999999\", \"YOUR_OTP_ID\")\n    print(\"OTP send:\", res[\"ok\"], res[\"message\"])\n\n    code = input(\"Enter the OTP: \").strip()\n    res = f2s.verify_otp(\"9999999999\", code)\n    print(\"Verified\" if res[\"ok\"] else f\"Not verified: {res['message']}\")<\/code><\/pre>\n<p>The DLR API replies with <code>success<\/code> instead of <code>return<\/code>, so <code>_call<\/code> accepts either. Fast2SMS also has Python starter kits for Flask and Django with this client, a login page, a mock server and tests, if you want a ready project.<\/p>\n<h2>Common problems and fixes<\/h2>\n<ul>\n<li><strong>KeyError: &#8216;FAST2SMS_API_KEY&#8217;:<\/strong> the environment variable is not set for the user or service that runs your app (systemd, gunicorn, cron). Set it there.<\/li>\n<li><strong>Error 414 only in production:<\/strong> API Security is on and the production server&#8217;s public IP is not whitelisted.<\/li>\n<li><strong>SSLError on old servers:<\/strong> update <code>certifi<\/code> and your OS CA certificates. Do not use <code>verify=False<\/code>.<\/li>\n<li><strong>Hindi variable values break:<\/strong> pass normal Python strings and let <code>requests<\/code> send JSON. Do not encode them yourself.<\/li>\n<li><strong>Celery task sends twice:<\/strong> a task retry after a timeout resends the SMS. Turn off automatic retries for send tasks.<\/li>\n<\/ul>\n<section class=\"f2s-faq\">\n<h2>Frequently asked questions<\/h2>\n<h3>Is there an official Fast2SMS pip package?<\/h3>\n<p>You do not need one. The API is JSON over HTTPS, and the small class above covers DLT SMS, OTP and delivery reports.<\/p>\n<h3>Can I use httpx or aiohttp instead of requests?<\/h3>\n<p>Yes. Send the same JSON body and the <code>authorization<\/code> header. The endpoints do not depend on the client library.<\/p>\n<h3>How do I send different messages to different numbers in one call?<\/h3>\n<p>Use <code>POST \/dev\/custom<\/code> with a <code>requests<\/code> list, one item per message. For the same text to many numbers, one bulkV2 call with comma-separated numbers is enough.<\/p>\n<h3>Can I send OTP with my own DLT template instead of Smart OTP?<\/h3>\n<p>Yes. Send your approved OTP template with <code>route=dlt<\/code> and the code in <code>variables_values<\/code>. Your Python app must then create, store and check the code itself.<\/p>\n<h3>Is verify charged?<\/h3>\n<p>No. Only sends and resends are charged.<\/p>\n<h3>How long can an OTP stay valid?<\/h3>\n<p>Set <code>otp_expiry<\/code> in minutes, from 1 to 10080. The default is 15 minutes. For login, 5 to 10 minutes is common.<\/p>\n<\/section>\n<aside class=\"f2s-cta\" aria-label=\"Get started with Fast2SMS\"><div class=\"f2s-cta__copy\"><strong class=\"f2s-cta__title\">Build OTP login in Python<\/strong><p>Create a Smart OTP template, copy the OTP ID and use the client above in Flask, Django or FastAPI.<\/p><\/div><div class=\"f2s-cta__actions\"><a class=\"f2s-cta__btn f2s-cta__btn--primary\" href=\"https:\/\/www.fast2sms.com\/\"><span>Sign up free<\/span> <span class=\"f2sh-btn__arrow\" aria-hidden=\"true\">&rarr;<\/span><\/a><a class=\"f2s-cta__btn f2s-cta__btn--ghost\" href=\"https:\/\/www.fast2sms.com\/meet\/\" target=\"_blank\" rel=\"noopener\">Book a free meeting<\/a><\/div><\/aside>\n<h2>Related guides<\/h2>\n<ul>\n<li><a href=\"\/help\/dlt-sms-api-guide\/\">Fast2SMS DLT SMS API: send SMS with your DLT template<\/a><\/li>\n<li><a href=\"\/help\/smart-otp-send-otp-api\/\">Fast2SMS Send OTP API: parameters and code examples<\/a><\/li>\n<li><a href=\"\/help\/smart-otp-resend-otp-api\/\">Fast2SMS Resend OTP API<\/a><\/li>\n<li><a href=\"\/help\/fast2sms-webhooks-setup-delivery-reports\/\">Webhooks in Fast2SMS: get delivery reports on your own URL<\/a><\/li>\n<li><a href=\"\/help\/fast2sms-api-error-codes\/\">Fast2SMS API error codes and fixes<\/a><\/li>\n<li><a href=\"https:\/\/www.fast2sms.com\/bulk-sms\">Fast2SMS Bulk SMS<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>You can send SMS and OTP from Python with the Fast2SMS API using the requests library and a few lines of code. This guide covers a DLT SMS from your own sender ID, a login OTP with Smart OTP (send, verify and resend), delivery reports by API and by webhook, and error handling. The last [&hellip;]<\/p>\n","protected":false},"featured_media":18072,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,13,5],"tags":[],"class_list":["post-18081","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-api","category-bulk-sms","category-otp"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Send SMS and OTP in Python: SMS API Code | Fast2SMS<\/title>\n<meta name=\"description\" content=\"Send DLT SMS and OTP from Python with the Fast2SMS API and requests. Smart OTP verify, delivery report, Flask webhook, error handling and a ready client.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.fast2sms.com\/help\/python-sms-api\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Send SMS and OTP in Python: SMS API Code | Fast2SMS\" \/>\n<meta property=\"og:description\" content=\"Send DLT SMS and OTP from Python with the Fast2SMS API and requests. Smart OTP verify, delivery report, Flask webhook, error handling and a ready client.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.fast2sms.com\/help\/python-sms-api\/\" \/>\n<meta property=\"og:site_name\" content=\"Fast2SMS\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Fast2SMS\/\" \/>\n<meta property=\"article:published_time\" content=\"2026-11-18T18:16:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-18T19:22:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.fast2sms.com\/help\/wp-content\/uploads\/2026\/11\/api-key-hd-13-scaled.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"2560\" \/>\n\t<meta property=\"og:image:height\" content=\"1600\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@fast2sms\" \/>\n<meta name=\"twitter:site\" content=\"@fast2sms\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/python-sms-api\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/python-sms-api\\\/\"},\"author\":{\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/#organization\"},\"headline\":\"Send SMS and OTP in Python with Fast2SMS API (requests)\",\"datePublished\":\"2026-11-18T18:16:38+00:00\",\"dateModified\":\"2026-11-18T19:22:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/python-sms-api\\\/\"},\"wordCount\":1482,\"publisher\":{\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/python-sms-api\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/api-key-hd-13-scaled.webp\",\"articleSection\":[\"API\",\"Bulk SMS\",\"OTP\"],\"inLanguage\":\"en-IN\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/python-sms-api\\\/\",\"url\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/python-sms-api\\\/\",\"name\":\"Send SMS and OTP in Python: SMS API Code | Fast2SMS\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/python-sms-api\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/python-sms-api\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/api-key-hd-13-scaled.webp\",\"datePublished\":\"2026-11-18T18:16:38+00:00\",\"dateModified\":\"2026-11-18T19:22:15+00:00\",\"description\":\"Send DLT SMS and OTP from Python with the Fast2SMS API and requests. Smart OTP verify, delivery report, Flask webhook, error handling and a ready client.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/python-sms-api\\\/#breadcrumb\"},\"inLanguage\":\"en-IN\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/python-sms-api\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-IN\",\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/python-sms-api\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/api-key-hd-13-scaled.webp\",\"contentUrl\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/api-key-hd-13-scaled.webp\",\"width\":2560,\"height\":1600,\"caption\":\"API Key page showing a masked Authorization Key with View and Reset buttons and the API Access switch on\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/python-sms-api\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"API\",\"item\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/api\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Send SMS and OTP in Python with Fast2SMS API (requests)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/#website\",\"url\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/\",\"name\":\"Fast2SMS\",\"description\":\"Help\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/#organization\"},\"inLanguage\":\"en-IN\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/#organization\",\"name\":\"Fast2SMS\",\"url\":\"https:\\\/\\\/www.fast2sms.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/#logo\",\"url\":\"https:\\\/\\\/www.fast2sms.com\\\/Fast2SMS.png\",\"contentUrl\":\"https:\\\/\\\/www.fast2sms.com\\\/Fast2SMS.png\",\"caption\":\"Fast2SMS\"},\"image\":{\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/#logo\"},\"email\":\"support@fast2sms.com\",\"telephone\":\"+91-6262778822\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/Fast2SMS\",\"https:\\\/\\\/www.instagram.com\\\/fast2sms\",\"https:\\\/\\\/x.com\\\/fast2sms\",\"https:\\\/\\\/www.youtube.com\\\/Fast2SMSofficial\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/fast2sms\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Send SMS and OTP in Python: SMS API Code | Fast2SMS","description":"Send DLT SMS and OTP from Python with the Fast2SMS API and requests. Smart OTP verify, delivery report, Flask webhook, error handling and a ready client.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.fast2sms.com\/help\/python-sms-api\/","og_locale":"en_US","og_type":"article","og_title":"Send SMS and OTP in Python: SMS API Code | Fast2SMS","og_description":"Send DLT SMS and OTP from Python with the Fast2SMS API and requests. Smart OTP verify, delivery report, Flask webhook, error handling and a ready client.","og_url":"https:\/\/www.fast2sms.com\/help\/python-sms-api\/","og_site_name":"Fast2SMS","article_publisher":"https:\/\/www.facebook.com\/Fast2SMS\/","article_published_time":"2026-11-18T18:16:38+00:00","article_modified_time":"2026-11-18T19:22:15+00:00","og_image":[{"width":2560,"height":1600,"url":"https:\/\/www.fast2sms.com\/help\/wp-content\/uploads\/2026\/11\/api-key-hd-13-scaled.webp","type":"image\/webp"}],"twitter_card":"summary_large_image","twitter_creator":"@fast2sms","twitter_site":"@fast2sms","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.fast2sms.com\/help\/python-sms-api\/#article","isPartOf":{"@id":"https:\/\/www.fast2sms.com\/help\/python-sms-api\/"},"author":{"@id":"https:\/\/www.fast2sms.com\/#organization"},"headline":"Send SMS and OTP in Python with Fast2SMS API (requests)","datePublished":"2026-11-18T18:16:38+00:00","dateModified":"2026-11-18T19:22:15+00:00","mainEntityOfPage":{"@id":"https:\/\/www.fast2sms.com\/help\/python-sms-api\/"},"wordCount":1482,"publisher":{"@id":"https:\/\/www.fast2sms.com\/#organization"},"image":{"@id":"https:\/\/www.fast2sms.com\/help\/python-sms-api\/#primaryimage"},"thumbnailUrl":"https:\/\/www.fast2sms.com\/help\/wp-content\/uploads\/2026\/11\/api-key-hd-13-scaled.webp","articleSection":["API","Bulk SMS","OTP"],"inLanguage":"en-IN"},{"@type":"WebPage","@id":"https:\/\/www.fast2sms.com\/help\/python-sms-api\/","url":"https:\/\/www.fast2sms.com\/help\/python-sms-api\/","name":"Send SMS and OTP in Python: SMS API Code | Fast2SMS","isPartOf":{"@id":"https:\/\/www.fast2sms.com\/help\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.fast2sms.com\/help\/python-sms-api\/#primaryimage"},"image":{"@id":"https:\/\/www.fast2sms.com\/help\/python-sms-api\/#primaryimage"},"thumbnailUrl":"https:\/\/www.fast2sms.com\/help\/wp-content\/uploads\/2026\/11\/api-key-hd-13-scaled.webp","datePublished":"2026-11-18T18:16:38+00:00","dateModified":"2026-11-18T19:22:15+00:00","description":"Send DLT SMS and OTP from Python with the Fast2SMS API and requests. Smart OTP verify, delivery report, Flask webhook, error handling and a ready client.","breadcrumb":{"@id":"https:\/\/www.fast2sms.com\/help\/python-sms-api\/#breadcrumb"},"inLanguage":"en-IN","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.fast2sms.com\/help\/python-sms-api\/"]}]},{"@type":"ImageObject","inLanguage":"en-IN","@id":"https:\/\/www.fast2sms.com\/help\/python-sms-api\/#primaryimage","url":"https:\/\/www.fast2sms.com\/help\/wp-content\/uploads\/2026\/11\/api-key-hd-13-scaled.webp","contentUrl":"https:\/\/www.fast2sms.com\/help\/wp-content\/uploads\/2026\/11\/api-key-hd-13-scaled.webp","width":2560,"height":1600,"caption":"API Key page showing a masked Authorization Key with View and Reset buttons and the API Access switch on"},{"@type":"BreadcrumbList","@id":"https:\/\/www.fast2sms.com\/help\/python-sms-api\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.fast2sms.com\/help\/"},{"@type":"ListItem","position":2,"name":"API","item":"https:\/\/www.fast2sms.com\/help\/api\/"},{"@type":"ListItem","position":3,"name":"Send SMS and OTP in Python with Fast2SMS API (requests)"}]},{"@type":"WebSite","@id":"https:\/\/www.fast2sms.com\/help\/#website","url":"https:\/\/www.fast2sms.com\/help\/","name":"Fast2SMS","description":"Help","publisher":{"@id":"https:\/\/www.fast2sms.com\/#organization"},"inLanguage":"en-IN"},{"@type":"Organization","@id":"https:\/\/www.fast2sms.com\/#organization","name":"Fast2SMS","url":"https:\/\/www.fast2sms.com\/","logo":{"@type":"ImageObject","@id":"https:\/\/www.fast2sms.com\/#logo","url":"https:\/\/www.fast2sms.com\/Fast2SMS.png","contentUrl":"https:\/\/www.fast2sms.com\/Fast2SMS.png","caption":"Fast2SMS"},"image":{"@id":"https:\/\/www.fast2sms.com\/#logo"},"email":"support@fast2sms.com","telephone":"+91-6262778822","sameAs":["https:\/\/www.facebook.com\/Fast2SMS","https:\/\/www.instagram.com\/fast2sms","https:\/\/x.com\/fast2sms","https:\/\/www.youtube.com\/Fast2SMSofficial","https:\/\/www.linkedin.com\/company\/fast2sms"]}]}},"_links":{"self":[{"href":"https:\/\/www.fast2sms.com\/help\/wp-json\/wp\/v2\/posts\/18081","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.fast2sms.com\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fast2sms.com\/help\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fast2sms.com\/help\/wp-json\/wp\/v2\/comments?post=18081"}],"version-history":[{"count":1,"href":"https:\/\/www.fast2sms.com\/help\/wp-json\/wp\/v2\/posts\/18081\/revisions"}],"predecessor-version":[{"id":19390,"href":"https:\/\/www.fast2sms.com\/help\/wp-json\/wp\/v2\/posts\/18081\/revisions\/19390"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.fast2sms.com\/help\/wp-json\/wp\/v2\/media\/18072"}],"wp:attachment":[{"href":"https:\/\/www.fast2sms.com\/help\/wp-json\/wp\/v2\/media?parent=18081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fast2sms.com\/help\/wp-json\/wp\/v2\/categories?post=18081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fast2sms.com\/help\/wp-json\/wp\/v2\/tags?post=18081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}