Many AI agent use cases require registering for external services programmatically. This guide shows a complete account registration automation using Playwright and AgentMailr.
The Full Flow
- Create a dedicated inbox for this registration
- Navigate to the signup page and fill in the form
- Wait for the verification email to arrive
- Extract the OTP or click the verification link
- Complete signup and store the account credentials
Complete Code
import { chromium } from "playwright";
const BASE = "https://api.agentmailr.com";
const KEY = process.env.AGENTMAILR_API_KEY;
const h = { "X-API-Key": KEY, "Content-Type": "application/json" };
async function registerAccount(targetUrl) {
// Step 1: Create inbox
const inbox = await fetch(`${BASE}/v1/inboxes`, {
method: "POST", headers: h, body: "{}"
}).then(r => r.json());
// Step 2: Fill out registration form
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto(targetUrl);
await page.fill('[name="email"]', inbox.address);
await page.fill('[name="password"]', generatePassword());
await page.click('[type="submit"]');
// Step 3: Wait for verification email (blocks until arrival)
const email = await fetch(
`${BASE}/v1/inboxes/${inbox.id}/wait?timeout=90`,
{ headers: h }
).then(r => r.json());
// Step 4a: If OTP code
if (email.parsed.otp_codes.length > 0) {
const otp = email.parsed.otp_codes[0];
await page.fill('[name="code"]', otp);
await page.click("button:has-text('Verify')");
}
// Step 4b: If verification link
if (email.parsed.verification_links.length > 0) {
const link = email.parsed.verification_links[0];
await page.goto(link);
}
await browser.close();
return { email: inbox.address, inboxId: inbox.id };
}
Handling Both OTPs and Links
Different services use different verification methods. The code above handles both: if otp_codes is populated, it fills in a code field. If verification_links is populated, it navigates to the link. You rarely need to know in advance which method the service uses.
Storing Credentials
After successful registration, store inbox.id alongside the account credentials. You will need the inbox ID later if the service sends password reset or re-verification emails.