{"id":18026,"date":"2026-11-18T23:45:08","date_gmt":"2026-11-18T18:15:08","guid":{"rendered":"https:\/\/www.fast2sms.com\/help\/java-sms-api\/"},"modified":"2026-11-19T00:50:03","modified_gmt":"2026-11-18T19:20:03","slug":"java-sms-api","status":"publish","type":"post","link":"https:\/\/www.fast2sms.com\/help\/java-sms-api\/","title":{"rendered":"Send SMS and OTP in Java with Fast2SMS API (HttpClient, Spring Boot)"},"content":{"rendered":"<p>This guide shows how to send SMS and OTP from Java with the Fast2SMS API using the <code>java.net.http.HttpClient<\/code> that ships with Java 11 and later. You will send a DLT SMS, send and verify a <a href=\"\/help\/smart-otp-setup-guide-fast2sms\/\">Smart OTP<\/a>, read the delivery report, receive webhooks in Spring Boot and handle errors. The last section has one class you can copy into any Java or Spring 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 Java<\/strong><p>Get your API key and template IDs, then send DLT SMS and OTP from your Java backend.<\/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>Java 11 or later.<\/strong> The examples use Jackson for JSON, which Spring Boot already includes.<\/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>, and its <strong>Message ID<\/strong>.<\/li>\n<li><strong>OTP:<\/strong> a Smart OTP template and its <strong>OTP ID<\/strong>.<\/li>\n<li><strong>Wallet balance.<\/strong> SMS, OTP sends and resends are charged.<\/li>\n<li><strong>Server only.<\/strong> Keep the key in your backend. An Android app must call your server, not Fast2SMS.<\/li>\n<\/ul>\n<h2>Get the key and IDs<\/h2>\n<ol class=\"f2s-steps\">\n<li>\n<h3>API key<\/h3>\n<p>In <strong>Account Setting<\/strong> open <strong>API Key<\/strong>, click <strong>View<\/strong>, enter the OTP sent 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-10-scaled.webp\" width=\"1440\" height=\"900\" loading=\"lazy\" decoding=\"async\" alt=\"API Key page with the masked Authorization Key, View and Reset buttons and the API Access switch on\"><figcaption>Put this in an environment variable or a secret store.<\/figcaption><\/figure>\n<\/li>\n<li>\n<h3>Message ID and sender ID<\/h3>\n<p>In <strong>DLT Manager<\/strong>, <strong>Content Template<\/strong>, copy the <strong>Message ID<\/strong> and <strong>Sender ID<\/strong> of an approved template.<\/p>\n<figure class=\"f2s-shot\"><img src=\"https:\/\/www.fast2sms.com\/help\/wp-content\/uploads\/2026\/11\/sms-dlt-template-list-hd-12-scaled.webp\" width=\"1440\" height=\"900\" loading=\"lazy\" decoding=\"async\" alt=\"DLT Manager Content Template cards with approved templates, Message ID, Sender ID, Template ID and Entity\"><figcaption>Both values come from the same card.<\/figcaption><\/figure>\n<\/li>\n<li>\n<h3>OTP ID<\/h3>\n<p>In Smart OTP, <strong>OTP Template List<\/strong>, copy the <strong>OTP ID<\/strong> of your login template.<\/p>\n<figure class=\"f2s-shot\"><img src=\"https:\/\/www.fast2sms.com\/help\/wp-content\/uploads\/2026\/11\/smart-otp-template-list-hd-7-scaled.webp\" width=\"1440\" height=\"900\" loading=\"lazy\" decoding=\"async\" alt=\"Smart OTP Template List cards showing OTP ID, sender or WhatsApp number, template ID and fallback\"><figcaption>The OTP ID is 10 characters long.<\/figcaption><\/figure>\n<\/li>\n<\/ol>\n<h2>Set up<\/h2>\n<p>Maven dependency for JSON (skip it in Spring Boot, which has Jackson already):<\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n  &lt;groupId&gt;com.fasterxml.jackson.core&lt;\/groupId&gt;\n  &lt;artifactId&gt;jackson-databind&lt;\/artifactId&gt;\n  &lt;version&gt;2.17.2&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>Gradle: <code>implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.2'<\/code>. Set the key as an environment variable:<\/p>\n<pre><code class=\"language-bash\">export FAST2SMS_API_KEY=\"YOUR_API_KEY\"<\/code><\/pre>\n<p>A shared helper that posts JSON and reads the reply:<\/p>\n<pre><code class=\"language-java\">import com.fasterxml.jackson.databind.JsonNode;\nimport com.fasterxml.jackson.databind.ObjectMapper;\nimport java.net.URI;\nimport java.net.http.*;\nimport java.time.Duration;\nimport java.util.Map;\n\npublic class F2s {\n    static final HttpClient HTTP = HttpClient.newBuilder()\n            .connectTimeout(Duration.ofSeconds(5)).build();\n    static final ObjectMapper JSON = new ObjectMapper();\n    static final String KEY = System.getenv(\"FAST2SMS_API_KEY\");\n\n    static JsonNode post(String path, Map&lt;String, Object&gt; body) throws Exception {\n        HttpRequest req = HttpRequest.newBuilder()\n                .uri(URI.create(\"https:\/\/www.fast2sms.com\" + path))\n                .timeout(Duration.ofSeconds(15))\n                .header(\"authorization\", KEY)\n                .header(\"Content-Type\", \"application\/json\")\n                .header(\"Accept\", \"application\/json\")\n                .POST(HttpRequest.BodyPublishers.ofString(JSON.writeValueAsString(body)))\n                .build();\n        HttpResponse&lt;String&gt; res = HTTP.send(req, HttpResponse.BodyHandlers.ofString());\n        return JSON.readTree(res.body());\n    }\n}<\/code><\/pre>\n<div class=\"f2s-note\">Pass the bare key in the <code>authorization<\/code> header. Do not prefix &#8220;Bearer &#8220;. The OTP endpoints keep the prefix as part of the key and fail with &#8220;Invalid authorization key&#8221;.<\/div>\n<h2>Send a DLT SMS in Java<\/h2>\n<p>Call <code>\/dev\/bulkV2<\/code> with <code>route=dlt<\/code>, your sender ID, the template&#8217;s Message ID, the variable values and the numbers. The example template is Message ID 10001 on DEMOTR with four variables.<\/p>\n<pre><code class=\"language-java\">JsonNode r = F2s.post(\"\/dev\/bulkV2\", Map.of(\n        \"route\", \"dlt\",\n        \"sender_id\", \"DEMOTR\",                       \/\/ exact case\n        \"message\", \"10001\",                          \/\/ Message ID, not the Template ID\n        \"variables_values\", String.join(\"|\", \"Rahul\", \"DT10458\", \"1240\", \"21 Nov\"),\n        \"numbers\", String.join(\",\", \"9999999999\", \"8888888888\"),\n        \"udf1\", \"order-DT10458\"));\n\nif (r.path(\"return\").asBoolean(false)) {\n    System.out.println(\"Queued: \" + r.path(\"request_id\").asText());   \/\/ S...\n} else {\n    System.out.println(\"Not sent: \" + r.path(\"status_code\").asText() + \" \" + r.path(\"message\"));\n}<\/code><\/pre>\n<ul>\n<li>Variable values go in template order, joined with <code>|<\/code>, and each is cut to 40 characters.<\/li>\n<li>The sender ID is case-sensitive.<\/li>\n<li>Success: <code>\"message\": [\"SMS sent successfully.\"]<\/code>, an array. It means queued.<\/li>\n<li>Schedule with <code>\"schedule_time\", \"25-11-2026-10-30\"<\/code> (DD-MM-YYYY-HH-MM, IST). <code>Map.of<\/code> takes up to 10 pairs; use a <code>HashMap<\/code> for more.<\/li>\n<\/ul>\n<h2>Send and verify an OTP<\/h2>\n<pre><code class=\"language-java\">\/\/ Send\nJsonNode sent = F2s.post(\"\/dev\/otp\/send\", Map.of(\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\nSystem.out.println(sent.path(\"return\").asBoolean() ? \"OTP sent\" : sent.path(\"message\").asText());\n\n\/\/ Verify\nJsonNode check = F2s.post(\"\/dev\/otp\/verify\", Map.of(\"mobile\", \"9999999999\", \"otp\", \"482913\"));\nif (check.path(\"return\").asBoolean()) {\n    System.out.println(\"Verified\");\n} else {\n    System.out.println(check.path(\"message\").asText());  \/\/ Invalid OTP, OTP expired, ...\n}\n\n\/\/ Resend the same OTP (charged again)\nJsonNode again = F2s.post(\"\/dev\/otp\/resend\", Map.of(\"mobile\", \"9999999999\"));<\/code><\/pre>\n<ul>\n<li>Verify uses only mobile and code. It checks the newest unverified OTP for that number.<\/li>\n<li>Sending a new OTP to a number cancels its older OTPs.<\/li>\n<li>Resend repeats the same code, within 10 minutes of the first send and up to 5 sends in total (the first one counts).<\/li>\n<li>Sends that come too fast get &#8220;Please wait N seconds before requesting a new OTP&#8221;. The wait is set in <strong>OTP Settings<\/strong>, default 10 seconds.<\/li>\n<\/ul>\n<h2>Get delivery reports<\/h2>\n<p>Choose either the DLR API or a webhook. Do not use both for the same messages: reading a DLR by API can stop the webhook for those rows.<\/p>\n<h3>DLR API<\/h3>\n<pre><code class=\"language-java\">HttpRequest req = HttpRequest.newBuilder()\n        .uri(URI.create(\"https:\/\/www.fast2sms.com\/dev\/dlr\/Sa1B2c3D4e5F6g7\"))\n        .timeout(Duration.ofSeconds(15))\n        .header(\"authorization\", F2s.KEY)\n        .GET().build();\nJsonNode dlr = F2s.JSON.readTree(F2s.HTTP.send(req, HttpResponse.BodyHandlers.ofString()).body());\n\nif (dlr.path(\"success\").asBoolean()) {\n    for (JsonNode row : dlr.path(\"data\").get(0).path(\"delivery_status\")) {\n        System.out.println(row.path(\"mobile\").asText() + \" \" + row.path(\"status\").asText());\n    }\n}<\/code><\/pre>\n<p>Status is <code>Delivered<\/code>, <code>Failed<\/code> or <code>Sent<\/code> (operator report pending). The API looks back 7 days.<\/p>\n<h3>Spring Boot webhook receiver<\/h3>\n<p>In the panel, open <strong>Webhook<\/strong>, then <strong>SMS<\/strong> or <strong>OTP<\/strong>, and add a webhook with 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-1-scaled.webp\" width=\"1440\" height=\"900\" loading=\"lazy\" decoding=\"async\" alt=\"CREATE WEBHOOK (SMS) modal with Webhook Name, Standard Webhook format, Select Event, Entity ID, POST method, URL and JSON Content-Type, and the JSON body fields\"><figcaption>The Standard body sends request_id, mobile, status and more.<\/figcaption><\/figure>\n<pre><code class=\"language-java\">import org.springframework.http.ResponseEntity;\nimport org.springframework.web.bind.annotation.*;\nimport java.util.Map;\n\n@RestController\npublic class Fast2smsWebhookController {\n\n    @PostMapping(\"\/fast2sms\/webhook\")\n    public ResponseEntity&lt;String&gt; receive(\n            @RequestHeader(value = \"X-Webhook-Token\", required = false) String token,\n            @RequestBody Map&lt;String, Object&gt; body) {\n\n        \/\/ Optional: a header you added in the webhook's Headers tab\n        if (token == null || &#33;token.equals(System.getenv(\"MY_WEBHOOK_TOKEN\"))) {\n            return ResponseEntity.status(401).build();\n        }\n        String requestId = String.valueOf(body.get(\"request_id\"));\n        String mobile    = String.valueOf(body.get(\"mobile\"));\n        String status    = String.valueOf(body.get(\"status\"));   \/\/ delivered or failed\n        \/\/ save the status, then reply fast\n        return ResponseEntity.ok(\"OK\");\n    }\n}<\/code><\/pre>\n<p>Reply with 2xx within 10 seconds. On timeouts, 408, 429 or 5xx, Fast2SMS retries up to 3 times, 60 seconds apart. Webhooks fire for messages sent through the API, when the report arrives within 24 hours. See each attempt in <strong>Webhook Logs<\/strong>.<\/p>\n<aside class=\"f2s-cta\" aria-label=\"Get started with Fast2SMS\"><div class=\"f2s-cta__copy\"><strong class=\"f2s-cta__title\">Track SMS delivery in your Java app<\/strong><p>Set up a webhook and get delivered or failed status for every API message on your Spring Boot 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>Read <code>return<\/code> in the JSON<\/strong>, not only the HTTP status. Low balance can come with HTTP 200 and <code>\"return\": false<\/code> (status_code 416).<\/li>\n<li><strong>Do not retry sends automatically.<\/strong> An <code>HttpTimeoutException<\/code> after the request reached Fast2SMS may still mean the SMS goes out. A retry can send and charge twice. Retry once only on <code>ConnectException<\/code>. Verify can be retried once.<\/li>\n<li><strong>Use the panel&#8217;s API Logs<\/strong> to compare what you sent with what came back.<\/li>\n<\/ul>\n<div class=\"f2s-facts\">\n<table>\n<thead>\n<tr>\n<th>Reply<\/th>\n<th>Fix<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>412 Invalid Authentication, Check Authorization Key (HTTP 401)<\/td>\n<td>The key is wrong, reset or <code>null<\/code> because the env variable is missing.<\/td>\n<\/tr>\n<tr>\n<td>Invalid authorization key (OTP, HTTP 401)<\/td>\n<td>Wrong key or a &#8220;Bearer &#8221; prefix.<\/td>\n<\/tr>\n<tr>\n<td>413 Authorization Key Disabled<\/td>\n<td>Turn <strong>API Access<\/strong> on.<\/td>\n<\/tr>\n<tr>\n<td>414 IP is blacklisted from Dev API section<\/td>\n<td>Whitelist the server IP in <strong>API Security<\/strong>.<\/td>\n<\/tr>\n<tr>\n<td>406 Invalid Sender ID<\/td>\n<td>Use the approved sender, same case.<\/td>\n<\/tr>\n<tr>\n<td>424 Invalid Message ID<\/td>\n<td>Use the Message ID linked to that sender.<\/td>\n<\/tr>\n<tr>\n<td>411 Invalid Numbers<\/td>\n<td>Every number was invalid.<\/td>\n<\/tr>\n<tr>\n<td>416 You don&#8217;t have sufficient wallet 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<\/tr>\n<tr>\n<td>Please wait N seconds before requesting a new OTP<\/td>\n<td>Wait; show a timer.<\/td>\n<\/tr>\n<tr>\n<td>Invalid OTP \/ OTP expired \/ Maximum verification attempts reached<\/td>\n<td>Retry, or send a new OTP.<\/td>\n<\/tr>\n<tr>\n<td>OTP not found or already verified (HTTP 404)<\/td>\n<td>Send a new OTP.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2>Full copy-paste example: Fast2SMS.java<\/h2>\n<pre><code class=\"language-java\">import com.fasterxml.jackson.databind.JsonNode;\nimport com.fasterxml.jackson.databind.ObjectMapper;\nimport java.net.ConnectException;\nimport java.net.URI;\nimport java.net.URLEncoder;\nimport java.net.http.*;\nimport java.nio.charset.StandardCharsets;\nimport java.time.Duration;\nimport java.util.*;\n\npublic final class Fast2SMS {\n\n    public record Result(boolean ok, int http, String statusCode, String requestId, String message, JsonNode raw) {}\n\n    private static final String BASE = \"https:\/\/www.fast2sms.com\";\n    private final HttpClient http = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).build();\n    private final ObjectMapper json = new ObjectMapper();\n    private final String apiKey;\n\n    public Fast2SMS(String apiKey) {\n        this.apiKey = Objects.requireNonNull(apiKey, \"FAST2SMS_API_KEY is not set\");\n    }\n\n    public Fast2SMS() {\n        this(System.getenv(\"FAST2SMS_API_KEY\"));\n    }\n\n    public Result sendDlt(String senderId, String messageId, List&lt;String&gt; vars, List&lt;String&gt; numbers) {\n        Map&lt;String, Object&gt; b = new HashMap&lt;&gt;();\n        b.put(\"route\", \"dlt\");\n        b.put(\"sender_id\", senderId);\n        b.put(\"message\", messageId);\n        b.put(\"variables_values\", String.join(\"|\", vars));\n        b.put(\"numbers\", String.join(\",\", numbers));\n        return call(\"POST\", \"\/dev\/bulkV2\", b);\n    }\n\n    public Result sendOtp(String mobile, String otpId, int expiryMinutes) {\n        return call(\"POST\", \"\/dev\/otp\/send\", Map.of(\"mobile\", mobile, \"otp_id\", otpId, \"otp_expiry\", expiryMinutes));\n    }\n\n    public Result verifyOtp(String mobile, String otp) {\n        return call(\"POST\", \"\/dev\/otp\/verify\", Map.of(\"mobile\", mobile, \"otp\", otp));\n    }\n\n    public Result resendOtp(String mobile) {\n        return call(\"POST\", \"\/dev\/otp\/resend\", Map.of(\"mobile\", mobile));\n    }\n\n    public Result deliveryReport(String requestId) {\n        return call(\"GET\", \"\/dev\/dlr\/\" + URLEncoder.encode(requestId, StandardCharsets.UTF_8), null);\n    }\n\n    private Result call(String method, String path, Map&lt;String, Object&gt; body) {\n        try {\n            HttpRequest.Builder rb = HttpRequest.newBuilder(URI.create(BASE + path))\n                    .timeout(Duration.ofSeconds(15))\n                    .header(\"authorization\", apiKey)\n                    .header(\"Accept\", \"application\/json\");\n            if (\"POST\".equals(method)) {\n                rb.header(\"Content-Type\", \"application\/json\")\n                  .POST(HttpRequest.BodyPublishers.ofString(json.writeValueAsString(body)));\n            } else {\n                rb.GET();\n            }\n            HttpResponse&lt;String&gt; res = http.send(rb.build(), HttpResponse.BodyHandlers.ofString());\n            JsonNode n;\n            try {\n                n = json.readTree(res.body());\n            } catch (Exception e) {\n                return new Result(false, res.statusCode(), null, null, \"Non-JSON response\", null);\n            }\n            boolean ok = res.statusCode() \/ 100 == 2\n                    &amp;&amp; (n.path(\"return\").asBoolean(false) || n.path(\"success\").asBoolean(false));\n            JsonNode m = n.path(\"message\");\n            String msg = m.isArray() ? String.join(\" \", json.convertValue(m, String[].class)) : m.asText(\"\");\n            return new Result(ok, res.statusCode(), n.path(\"status_code\").asText(null),\n                    n.path(\"request_id\").asText(null), msg, n);\n        } catch (ConnectException e) {\n            return new Result(false, 0, null, null, \"Could not connect: \" + e.getMessage(), null);\n        } catch (Exception e) {\n            return new Result(false, 0, null, null, \"Request failed: \" + e.getMessage(), null);\n        }\n    }\n\n    public static void main(String[] args) {\n        Fast2SMS f2s = new Fast2SMS();\n\n        Result sms = f2s.sendDlt(\"DEMOTR\", \"10001\", List.of(\"Rahul\", \"DT10458\", \"1240\", \"21 Nov\"), List.of(\"9999999999\"));\n        System.out.println(sms.ok() ? \"SMS queued: \" + sms.requestId() : \"SMS error: \" + sms.message());\n\n        Result otp = f2s.sendOtp(\"9999999999\", \"YOUR_OTP_ID\", 5);\n        System.out.println(otp.ok() ? \"OTP sent\" : \"OTP error: \" + otp.message());\n\n        Result v = f2s.verifyOtp(\"9999999999\", \"482913\");\n        System.out.println(v.ok() ? \"Verified\" : \"Not verified: \" + v.message());\n    }\n}<\/code><\/pre>\n<p>The record type needs Java 16 or later; on Java 11, turn <code>Result<\/code> into a small class. The DLR API answers with <code>success<\/code> instead of <code>return<\/code>, so <code>call<\/code> checks both. For a ready project, the Fast2SMS starter kit for Spring Boot has this client with send, verify and resend endpoints, a mock server and tests.<\/p>\n<h2>Common problems and fixes<\/h2>\n<ul>\n<li><strong>NullPointerException on the key:<\/strong> <code>System.getenv<\/code> returned null. Set the variable for the user that runs the JVM, or load it from Spring&#8217;s <code>application.properties<\/code> through an environment placeholder.<\/li>\n<li><strong>Error 414 on the production server:<\/strong> whitelist that server&#8217;s public IP in <strong>API Security<\/strong>.<\/li>\n<li><strong>PKIX path building failed:<\/strong> the JVM&#8217;s trust store is old. Update the JDK. Do not disable certificate checks.<\/li>\n<li><strong>Hindi text breaks:<\/strong> keep source files and the database in UTF-8. <code>BodyPublishers.ofString<\/code> sends UTF-8 by default.<\/li>\n<li><strong>Duplicate SMS:<\/strong> a Spring Retry or Resilience4j retry resent after a timeout. Exclude send calls from retries.<\/li>\n<\/ul>\n<section class=\"f2s-faq\">\n<h2>Frequently asked questions<\/h2>\n<h3>Is there a Fast2SMS Java SDK?<\/h3>\n<p>You do not need one. The API is HTTPS with JSON, and the class above uses the HttpClient built into Java.<\/p>\n<h3>Can I use OkHttp, RestTemplate or WebClient?<\/h3>\n<p>Yes. Send the same JSON body with the <code>authorization<\/code> header. Any HTTP client works.<\/p>\n<h3>Can my Android app call Fast2SMS directly?<\/h3>\n<p>No. The key would be inside the APK. The app should call your server, and your server calls Fast2SMS.<\/p>\n<h3>How do I send the same SMS to many numbers?<\/h3>\n<p>Put them comma-separated in <code>numbers<\/code>. Invalid numbers are skipped and not charged.<\/p>\n<h3>Is the verify call charged?<\/h3>\n<p>No. Send and resend are charged; verify is free.<\/p>\n<h3>Can I use my own OTP value?<\/h3>\n<p>Yes. Add <code>otp<\/code> (up to 10 characters) to the send call. Fast2SMS still checks it on verify.<\/p>\n<\/section>\n<aside class=\"f2s-cta\" aria-label=\"Get started with Fast2SMS\"><div class=\"f2s-cta__copy\"><strong class=\"f2s-cta__title\">Add OTP login to your Java app<\/strong><p>Create a Smart OTP template, copy the OTP ID and plug the class above into Spring Boot or any Java backend.<\/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-verify-otp-api\/\">Fast2SMS Verify OTP API: how verification works<\/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\/otp-sms\">Fast2SMS OTP SMS<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This guide shows how to send SMS and OTP from Java with the Fast2SMS API using the java.net.http.HttpClient that ships with Java 11 and later. You will send a DLT SMS, send and verify a Smart OTP, read the delivery report, receive webhooks in Spring Boot and handle errors. The last section has one class [&hellip;]<\/p>\n","protected":false},"featured_media":18018,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,13,5],"tags":[],"class_list":["post-18026","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 Java: SMS API Code | Fast2SMS<\/title>\n<meta name=\"description\" content=\"Send DLT SMS and OTP from Java with the Fast2SMS API and HttpClient. Smart OTP verify, delivery report, Spring Boot webhook, errors and a ready class.\" \/>\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\/java-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 Java: SMS API Code | Fast2SMS\" \/>\n<meta property=\"og:description\" content=\"Send DLT SMS and OTP from Java with the Fast2SMS API and HttpClient. Smart OTP verify, delivery report, Spring Boot webhook, errors and a ready class.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.fast2sms.com\/help\/java-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:15:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-18T19:20:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.fast2sms.com\/help\/wp-content\/uploads\/2026\/11\/api-key-hd-10-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\\\/java-sms-api\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/java-sms-api\\\/\"},\"author\":{\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/#organization\"},\"headline\":\"Send SMS and OTP in Java with Fast2SMS API (HttpClient, Spring Boot)\",\"datePublished\":\"2026-11-18T18:15:08+00:00\",\"dateModified\":\"2026-11-18T19:20:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/java-sms-api\\\/\"},\"wordCount\":1208,\"publisher\":{\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/java-sms-api\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/api-key-hd-10-scaled.webp\",\"articleSection\":[\"API\",\"Bulk SMS\",\"OTP\"],\"inLanguage\":\"en-IN\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/java-sms-api\\\/\",\"url\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/java-sms-api\\\/\",\"name\":\"Send SMS and OTP in Java: SMS API Code | Fast2SMS\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/java-sms-api\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/java-sms-api\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/api-key-hd-10-scaled.webp\",\"datePublished\":\"2026-11-18T18:15:08+00:00\",\"dateModified\":\"2026-11-18T19:20:03+00:00\",\"description\":\"Send DLT SMS and OTP from Java with the Fast2SMS API and HttpClient. Smart OTP verify, delivery report, Spring Boot webhook, errors and a ready class.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/java-sms-api\\\/#breadcrumb\"},\"inLanguage\":\"en-IN\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/java-sms-api\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-IN\",\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/java-sms-api\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/api-key-hd-10-scaled.webp\",\"contentUrl\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/api-key-hd-10-scaled.webp\",\"width\":2560,\"height\":1600,\"caption\":\"API Key page with the masked Authorization Key, View and Reset buttons and the API Access switch on\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.fast2sms.com\\\/help\\\/java-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 Java with Fast2SMS API (HttpClient, Spring Boot)\"}]},{\"@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 Java: SMS API Code | Fast2SMS","description":"Send DLT SMS and OTP from Java with the Fast2SMS API and HttpClient. Smart OTP verify, delivery report, Spring Boot webhook, errors and a ready class.","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\/java-sms-api\/","og_locale":"en_US","og_type":"article","og_title":"Send SMS and OTP in Java: SMS API Code | Fast2SMS","og_description":"Send DLT SMS and OTP from Java with the Fast2SMS API and HttpClient. Smart OTP verify, delivery report, Spring Boot webhook, errors and a ready class.","og_url":"https:\/\/www.fast2sms.com\/help\/java-sms-api\/","og_site_name":"Fast2SMS","article_publisher":"https:\/\/www.facebook.com\/Fast2SMS\/","article_published_time":"2026-11-18T18:15:08+00:00","article_modified_time":"2026-11-18T19:20:03+00:00","og_image":[{"width":2560,"height":1600,"url":"https:\/\/www.fast2sms.com\/help\/wp-content\/uploads\/2026\/11\/api-key-hd-10-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\/java-sms-api\/#article","isPartOf":{"@id":"https:\/\/www.fast2sms.com\/help\/java-sms-api\/"},"author":{"@id":"https:\/\/www.fast2sms.com\/#organization"},"headline":"Send SMS and OTP in Java with Fast2SMS API (HttpClient, Spring Boot)","datePublished":"2026-11-18T18:15:08+00:00","dateModified":"2026-11-18T19:20:03+00:00","mainEntityOfPage":{"@id":"https:\/\/www.fast2sms.com\/help\/java-sms-api\/"},"wordCount":1208,"publisher":{"@id":"https:\/\/www.fast2sms.com\/#organization"},"image":{"@id":"https:\/\/www.fast2sms.com\/help\/java-sms-api\/#primaryimage"},"thumbnailUrl":"https:\/\/www.fast2sms.com\/help\/wp-content\/uploads\/2026\/11\/api-key-hd-10-scaled.webp","articleSection":["API","Bulk SMS","OTP"],"inLanguage":"en-IN"},{"@type":"WebPage","@id":"https:\/\/www.fast2sms.com\/help\/java-sms-api\/","url":"https:\/\/www.fast2sms.com\/help\/java-sms-api\/","name":"Send SMS and OTP in Java: SMS API Code | Fast2SMS","isPartOf":{"@id":"https:\/\/www.fast2sms.com\/help\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.fast2sms.com\/help\/java-sms-api\/#primaryimage"},"image":{"@id":"https:\/\/www.fast2sms.com\/help\/java-sms-api\/#primaryimage"},"thumbnailUrl":"https:\/\/www.fast2sms.com\/help\/wp-content\/uploads\/2026\/11\/api-key-hd-10-scaled.webp","datePublished":"2026-11-18T18:15:08+00:00","dateModified":"2026-11-18T19:20:03+00:00","description":"Send DLT SMS and OTP from Java with the Fast2SMS API and HttpClient. Smart OTP verify, delivery report, Spring Boot webhook, errors and a ready class.","breadcrumb":{"@id":"https:\/\/www.fast2sms.com\/help\/java-sms-api\/#breadcrumb"},"inLanguage":"en-IN","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.fast2sms.com\/help\/java-sms-api\/"]}]},{"@type":"ImageObject","inLanguage":"en-IN","@id":"https:\/\/www.fast2sms.com\/help\/java-sms-api\/#primaryimage","url":"https:\/\/www.fast2sms.com\/help\/wp-content\/uploads\/2026\/11\/api-key-hd-10-scaled.webp","contentUrl":"https:\/\/www.fast2sms.com\/help\/wp-content\/uploads\/2026\/11\/api-key-hd-10-scaled.webp","width":2560,"height":1600,"caption":"API Key page with the masked Authorization Key, View and Reset buttons and the API Access switch on"},{"@type":"BreadcrumbList","@id":"https:\/\/www.fast2sms.com\/help\/java-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 Java with Fast2SMS API (HttpClient, Spring Boot)"}]},{"@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\/18026","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=18026"}],"version-history":[{"count":1,"href":"https:\/\/www.fast2sms.com\/help\/wp-json\/wp\/v2\/posts\/18026\/revisions"}],"predecessor-version":[{"id":19347,"href":"https:\/\/www.fast2sms.com\/help\/wp-json\/wp\/v2\/posts\/18026\/revisions\/19347"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.fast2sms.com\/help\/wp-json\/wp\/v2\/media\/18018"}],"wp:attachment":[{"href":"https:\/\/www.fast2sms.com\/help\/wp-json\/wp\/v2\/media?parent=18026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fast2sms.com\/help\/wp-json\/wp\/v2\/categories?post=18026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fast2sms.com\/help\/wp-json\/wp\/v2\/tags?post=18026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}