Ready-to-Use SMS OTP API Code Snippets for a Quick and Smooth Implementation
Our sample code isn’t just for show—it’s a tool you can use to address real-world challenges in your app or platform. Here are some scenarios where our code snippets come in handy.
Just replace ‘your_api_key’ with your API Key from your Dashboard and ‘1234567890’ with an actual phone number.
curl -X POST "https://api.myotp.app/generate_otp" \
-H "X-API-Key: your_api_key" \
-H "Content-Type: application/json" \
-d '{"phone_number":"1234567890"}'
const axios = require('axios');
axios.post('https://api.myotp.app/generate_otp', {
phone_number: '1234567890'
}, {
headers: {
'X-API-Key': 'your_api_key'
}
}).then(response => {
console.log(response.data);
}).catch(error => {
console.error(error);
});
import requests
headers = {
'X-API-Key': 'your_api_key',
}
data = {
'phone_number': '1234567890'
}
response = requests.post('https://api.myotp.app/generate_otp', headers=headers, json=data)
print(response.json())
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("X-API-Key", "your_api_key");
var content = new StringContent("{\"phone_number\":\"1234567890\"}", Encoding.UTF8, "application/json");
var response = await client.PostAsync("https://api.myotp.app/generate_otp", content);
var responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseString);
}
}
}
import okhttp3.*;
public class Main {
public static void main(String[] args) throws Exception {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\"phone_number\":\"1234567890\"}");
Request request = new Request.Builder()
.url("https://api.myotp.app/generate_otp")
.post(body)
.addHeader("X-API-Key", "your_api_key")
.addHeader("Content-Type", "application/json")
.build();
Response response = client.newCall(request).execute();
System.out.println(response.body().string());
}
}
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
)
func main() { url := "https://api.myotp.app/generate_otp"
var jsonStr = []byte(`{"phone_number":"1234567890"}`)
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("X-API-Key", "your_api_key")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, _ := client.Do(req)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}
require 'net/http'
require 'uri'
require 'json'
uri = URI.parse("https://api.myotp.app/generate_otp")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri, {
'Content-Type' => 'application/json',
'X-API-Key' => 'your_api_key'
})
request.body = {phone_number: '1234567890'}.to_json
response = http.request(request)
puts response.body
import 'package:http/http.dart' as http;
import 'dart:convert';
void generateOtp() async {
var url = 'https://api.myotp.app/generate_otp';
var headers = {
'X-API-Key': 'your_api_key',
'Content-Type': 'application/json'
};
var body = json.encode({
'phone_number': '1234567890'
});
var response = await http.post(url, headers: headers, body: body);
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');
}
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.myotp.app/generate_otp");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array("phone_number" => "1234567890")));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-API-Key: your_api_key',
'Content-Type: application/json'
));
$response = curl_exec($ch);
if ($response === false) {
throw new Exception('Curl error: ' . curl_error($ch));
}
curl_close($ch);
echo $response;
?>
function greet(name) {
console.log("Hello, " + name + "!");
}
greet("World");
These code snippets are plug-and-play. Just replace the placeholders with your actual API keys and endpoint URLs, and you’re good to go. For a deep dive, head over to our API Documentation.
If you run into any roadblocks or have further questions, don’t hesitate to reach out. Our developer support is here to help you every step of the way.
The team at MyOTP.App came together with a shared passion for simplifying
and democratizing two-factor authentication, and we’ve been working tirelessly ever
since to provide the best possible service to our clients.
© 2024 My OTP App by Broadnet | All rights reserved