Stagehand
US Postal Service 2FA login with Stagehand: step-by-step for agents
Your Stagehand agent gets to US Postal Service's “enter the code from your authenticator app” screen and stops. Here is how to make it type the right 6-digit code by itself, without putting the TOTP seed in your code, prompts or logs.
1 · Save the secret
In US Postal Service, turn on authenticator-app 2FA and click “Can't scan?” to reveal the setup key. Save it in your OTPilot vault — it becomes the account
us-postal-service. Detailed US Postal Service steps.2 · Create a scoped key
Create an agent key that can read only US Postal Service, and put it in the agent's environment as
OTPILOT_KEY.3 · Fetch the code in Stagehand
Fetch the code right before Stagehand fills the 2FA field, so it is always fresh.
Stagehand · typescript// npm i @browserbasehq/stagehand async function otp(account = "us-postal-service") { const r = await fetch(`https://otpilot.vibiz.app/api/v1/codes/${account}?reason=stagehand-login`, { headers: { Authorization: `Bearer ${process.env.OTPILOT_KEY}` }, }); const data = await r.json(); if (!r.ok) throw new Error(`${data.error.message} Fix: ${data.error.fix}`); if (data.status === "pending") { await new Promise((s) => setTimeout(s, 5000)); return otp(account); } return data.code as string; } await page.act("log into US Postal Service with the saved email and password"); const code = await otp(); await page.act(`type ${code} into the authenticator code field and submit`);
Good to know
- · The response includes
expires_in. If it is under ~4 seconds, usenext_codeso the code does not expire mid-submit. - · Turn on approval for US Postal Service if a human should tap Approve first; the API answers
202 pendinguntil they do. - · Every fetch is logged with the key, IP and the reason your agent passes, so you can audit what logged in and why.
Try it with your own secret — no signup
Paste a TOTP secret on the home page and get a live code plus a working API call in seconds.
US Postal Service with other frameworks
Use only with US Postal Service accounts you own and may automate. Not affiliated with US Postal Service or Stagehand.