Go Developer Guide

How to Send OTPs in Go

A robust technical guide to integrating the NepalOTP API into your Go backend. Learn how to handle secure HTTP requests using net/http and verify users securely in Nepal.

Implementing phone verification from scratch requires handling state, database expirations, random cryptographic generation, and managing retry attempts to prevent brute-force attacks.

By utilizing the NepalOTP REST API, all of this stateful logic is offloaded to our highly-available infrastructure. This guide will walk you through building a robust integration in Go.


1. Requesting the OTP

To send an OTP to a Nepali phone number, make a POST request to our /v1/otp/send endpoint. Pass the phone number in the body, and we will generate and deliver a 6-digit code to the user over the fastest local telecom routes.

HTTP Request Example
POST /v1/otp/send HTTP/1.1
Host: api.nepalotp.com
Authorization: Bearer YOUR_API_KEY
Content-Type: application/json

{
    "phone": "+9779812345678"
}

In your Go application, you should use the net/http package to construct this JSON payload and execute the request securely. Remember to store the id returned in the response object, as you will need it for the verification step.

2. Verifying the Code

When the user receives the SMS, they will input the 6-digit code into your frontend application. Your Go server then forwards these details to the /v1/otp/verify endpoint.

Verification Request
POST /v1/otp/verify HTTP/1.1
Host: api.nepalotp.com
Authorization: Bearer YOUR_API_KEY
Content-Type: application/json

{
    "id": "otp_8f9a7b6c5d4e3f2a",
    "code": "847293"
}

NepalOTP automatically checks if the code matches, if it has expired, or if the user has exhausted their maximum retry attempts. Based on the HTTP status code and the success boolean returned, your backend can either issue a JWT token (login successful) or return an error message to the client.

Ship authentication faster.

Get your sandbox API keys instantly and integrate our endpoints into your Go application today.

Create Developer Account