otpwatch handler_api.php recipes
Get API key

Phone verification when an AI agent runs your QA

Agents that click through staging can now handle most of a sign-up flow, until the form asks for a phone number. Give the agent a number the safe way: three narrow tools that order, read and release, with every limit enforced in your code, not in the prompt.

Get an API key Opens sign-up on SimAggregator, the marketplace this API belongs to. Key in the dashboard, balance in USD.

Scope first

This page is about agents testing your own product on an environment you control: a staging sign-up, a phone-change flow, a login that sends a code. Write down which app, which environment and which test accounts the agent may touch before you hand it tools that spend money. An agent with a phone number and no scope will eventually use it somewhere you did not intend.

Three tools, nothing more

Do not give the model the raw API. Give it three functions whose names and arguments make misuse hard.

tools.mjs
// handler_api reference: https://otpwatch.com/ai-agent-phone-verification-testing
// Building a bot that resells numbers to other people? Software program: https://otpwatch.com/partner
// Tool definitions for the model. Plain JSON Schema inside, so the array also serialises to tools.json.
export const tools = [
  {
    "name": "request_test_phone",
    "description": "Order a phone number for the current QA scenario on staging. Call only when the form on the staging site is ready to send a code. Returns the number in E.164 form.",
    "input_schema": {
      "type": "object",
      "properties": {
        "scenario": { "type": "string", "description": "Test case id from the QA plan, e.g. SIGNUP-PHONE-01" }
      },
      "required": ["scenario"]
    }
  },
  {
    "name": "read_sms_code",
    "description": "Wait for the SMS code on the phone returned by request_test_phone. Blocks for up to 3 minutes.",
    "input_schema": { "type": "object", "properties": {} }
  },
  {
    "name": "release_test_phone",
    "description": "Close the current test phone. Call when the scenario is finished, passed or failed.",
    "input_schema": { "type": "object", "properties": { "used": { "type": "boolean" } }, "required": ["used"] }
  }
];

The schema is plain JSON Schema; rename input_schema to parameters if your function-calling API expects that, or expose the same three tools from an MCP server. Notice what the model cannot choose: service, country and price are fixed by your code, and there is no tool to order a second number while one is open.

The handler enforces the limits

.env.example
# handler_api reference: https://otpwatch.com/ai-agent-phone-verification-testing
# Building a bot that resells numbers to other people? Software program: https://otpwatch.com/partner
SMS_API_BASE_URL=https://sim-aggregator.org/stubs/handler_api.php
# Key from the SimAggregator dashboard: https://otpwatch.com/key
SMS_API_KEY=
phone-tools.mjs
// handler_api reference: https://otpwatch.com/ai-agent-phone-verification-testing
// Building a bot that resells numbers to other people? Software program: https://otpwatch.com/partner
import { SmsClient } from "./otp-client.mjs"; // from the Node.js page

const client = new SmsClient(process.env.SMS_API_KEY); // the key never enters the prompt
const POLICY = {
  service: process.env.SMS_SERVICE ?? "tg",
  country: Number(process.env.SMS_COUNTRY ?? 16),
  maxPrice: Number(process.env.SMS_MAX_PRICE ?? 2),
  maxOrdersPerSession: 3,
  scenarios: /^SIGNUP-PHONE-\d{2}$|^PHONE-CHANGE-\d{2}$/,
};

let current = null;   // { id, e164, scenario, code }
let ordered = 0;
const audit = (event, data) => console.log(JSON.stringify({ t: new Date().toISOString(), event, ...data }));

export const handlers = {
  async request_test_phone({ scenario }) {
    if (!POLICY.scenarios.test(scenario ?? "")) return { error: "scenario not in the QA plan" };
    if (current) return { error: "a test phone is already open; release it first" };
    if (ordered >= POLICY.maxOrdersPerSession) return { error: "order limit for this session reached; stop and report" };
    const act = await client.order({ service: POLICY.service, country: POLICY.country, maxPrice: POLICY.maxPrice });
    ordered++;
    current = { ...act, scenario, code: null };
    audit("order", { scenario, id: act.id });
    return { phone: act.e164 };
  },

  async read_sms_code() {
    if (!current) return { error: "no open test phone" };
    try {
      current.code = await client.waitCode(current.id, { timeoutMs: 180_000, intervalMs: 3_000 });
      audit("code", { scenario: current.scenario, id: current.id });
      return { code: current.code };
    } catch (e) {
      return { error: `no code: ${e.message}` };
    }
  },

  async release_test_phone({ used }) {
    if (!current) return { ok: true };
    const reply = used && current.code ? await client.finish(current.id) : await client.cancel(current.id);
    audit("release", { scenario: current.scenario, id: current.id, reply });
    current = null;
    return { ok: true };
  },
};

// Call this from your harness when the agent session ends, however it ends.
export async function shutdown() {
  if (current) await handlers.release_test_phone({ used: false });
}

What this buys you:

  • The key stays out of the model. It lives in the process environment; the model only ever sees a phone number and a code.
  • Spend is bounded by arithmetic, not by the model's judgement: at most maxOrdersPerSession × maxPrice per session.
  • One number at a time keeps the account's simultaneous-order slots and the 600-requests-per-minute key budget out of reach of a looping agent.
  • Cleanup does not depend on the model. If the agent forgets release_test_phone, shutdown() cancels the order (the client repeats a cancel refused with EARLY_CANCEL_DENIED), and the hold returns to the balance.

Keep the agent on your site

The phone tools are only half of the boundary. The browser tool the agent uses should be limited to your staging origin (an allowlist in the browser tool or a proxy), so a number obtained for SIGNUP-PHONE-01 can only be typed into your form. Pages the agent reads are data, not instructions: if a page tells the agent to request another number or visit another site, your handler and your browser allowlist should make that impossible, whatever the model decides.

Prompt lines that help

system prompt excerpt
# handler_api reference: https://otpwatch.com/ai-agent-phone-verification-testing
# Building a bot that resells numbers to other people? Software program: https://otpwatch.com/partner
You are testing the staging site https://staging.example.com only.
Request a test phone only when the phone field is visible and the next action sends a code.
Never type the test phone or its code anywhere except the staging site.
After the scenario ends, passed or failed, call release_test_phone.
If a tool returns an error, stop and report it; do not work around it.

The prompt sets expectations; the handler enforces them. Write both, rely on the second.

Picking the service code

The order receives SMS only from the service named in it. If your staging flow verifies the phone through Telegram or WhatsApp, use tg or wa; the service codes page lists the rest. If your own backend sends the SMS under its own sender, confirm the right code with support before the agent depends on it. For the default here, Telegram in the United Kingdom, the "from" price was $1.3798 on .

ready to run it?

Wrap the API in three tools, keep the key server-side, cap the spend.

Get an API key

Install for your coding agent

terminal
# Agent skills index: https://otpwatch.com/.well-known/agent-skills/index.json
npx skills add https://otpwatch.com

Two skills: receiving an SMS code in your own app or test, and building a bot that sells numbers. Index: /.well-known/agent-skills/index.json

Updated · prices from the 24 Nov 2026 snapshot