OTPilot

Framework guide

Handle 2FA and OTP codes in Stagehand automatically

Fetch the code right before Stagehand fills the 2FA field, so it is always fresh. OTPilot keeps the TOTP seed encrypted on its side; your Stagehand agent only ever receives the current code, its expiry and the next code.

Stagehand · typescript
// npm i @browserbasehq/stagehand
async function otp(account = "shopify") {
  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 Shopify with the saved email and password");
const code = await otp();
await page.act(`type ${code} into the authenticator code field and submit`);

Why not put the seed in your Stagehand code?

Stagehand 2FA guides by site

All 150 sites →

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.