LOCALHOST
All pages
Tools

Phone Auth

Sign-in codes by text for your own app, in two calls.

Phone Auth texts a one-time code to a Zimbabwe mobile number and tells you whether what the person typed is right. Use it to sign people in, to confirm a number at sign-up, or before something that matters (a payout, a changed password). You never make, keep or compare codes yourself.

Codes come from LocalZW until you have a sender name of your own, cost what any text costs (from 1.8¢ at the best bundle rate, one credit each) and are paid from the account's SMS credits.

Start

  1. In the panel, open Developer → phone auth and press Start Phone Auth. You get an app (named after your business) and a key with the phone_auth:write permission, shown once.
  2. Keep the key in your backend's settings, for example as LH_PHONE_AUTH_KEY. It can send and check codes and nothing else; still, never put it in an app or page people download. Your app asks your server, your server asks us.
  3. Make the two calls below.

1. Send a code

curl https://api.localhost.co.zw/phone-auth/send \
  -H "Authorization: Bearer $LH_PHONE_AUTH_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: signup-42" \
  -d '{"phone": "0771234567", "reference": "user-42"}'

HTTP/1.1 202 Accepted
{"id": "9f1c2a…", "to": "+263771234567", "sender": "LocalZW", "expires_in": 300}

phone takes the number as people type it. app picks one of your apps (its id starts pa_); leave it out for the first. reference is yours, shown beside the code in the panel. Keep id with the person's session.

2. Check what they typed

curl https://api.localhost.co.zw/phone-auth/verify \
  -H "Authorization: Bearer $LH_PHONE_AUTH_KEY" \
  -H "Content-Type: application/json" \
  -d '{"id": "9f1c2a…", "code": "482913"}'

{"valid": true, "reason": ""}

Instead of id you may give phone: the latest code from that app to that number is checked. When valid is false, reason says why in words you can show: wrong, expired, used, too many tries, or no code sent.

The rules

RuleWhat happens
One code per number a minuteA second send within the minute answers 429. Show a "send again" button after 60 seconds.
Each code lasts5, 10, 15 minutes, set per app (5 by default).
5 triesThen the code stops working; send a new one.
Works onceA right code checked twice is valid: false the second time, even when both arrive together.
A new codeReplaces nothing: each has its own id. Checking by phone uses the newest.
Digits4, 6, 8, set per app (6 by default).
Out of credits402, and nothing is sent. Top up on the SMS page.
RetriesThe same Idempotency-Key returns the first answer and never texts twice.

Apps and sender names

Each app has the name the text uses (“482913 is your Gofasta code. It expires in 5 minutes. Don't share it.”), a sender, how many digits and how long a code lasts, and its own codes: a code sent by one app is never accepted by another. Add apps and change them in the panel; GET /phone-auth/apps lists them.

To send from your own name, apply for a sender name on the SMS page. Once the networks approve it, choose it for the app and every code after that comes from it.

In Node

const API = "https://api.localhost.co.zw/phone-auth";
const headers = { Authorization: `Bearer ${process.env.LH_PHONE_AUTH_KEY}`, "Content-Type": "application/json" };

export const sendCode = (phone) =>
  fetch(`${API}/send`, { method: "POST", headers, body: JSON.stringify({ phone }) }).then((r) => r.json());

export const checkCode = (id, code) =>
  fetch(`${API}/verify`, { method: "POST", headers, body: JSON.stringify({ id, code }) }).then((r) => r.json());

In Python

import os, httpx

API = "https://api.localhost.co.zw/phone-auth"
HEADERS = {"Authorization": f"Bearer {os.environ['LH_PHONE_AUTH_KEY']}"}

def send_code(phone: str) -> dict:
    return httpx.post(f"{API}/send", json={"phone": phone}, headers=HEADERS).json()

def check_code(otp_id: str, code: str) -> bool:
    return httpx.post(f"{API}/verify", json={"id": otp_id, "code": code}, headers=HEADERS).json()["valid"]

Every field of these calls is in the API reference. Assistants connected to the account can do the same with the send_phone_auth_code and check_phone_auth_code tools.