Embed Happierleads screens
Bring your existing clients across
A one-off script, run before launch, that gives every client you already have a website.
Clients who signed up before today have no website yet. Run this once before you launch: for each of them it does what sign-up does, then sets them to paid or trial like your billing webhook (trial or paid).
It is safe to run again: clients that already have a websiteId are skipped, and the id is saved before the next call, so a re-run only retries the failures.
const API = process.env.HL_API_BASE ?? 'https://rest-admin.happierleads.com';
const DRY_RUN = process.argv.includes('--dry-run');
async function hl(method, path, body) {
const r = await fetch(`${API}${path}?privateKey=${process.env.HL_MASTER_KEY}`, {
method,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
if (!r.ok) throw new Error(`${method} ${path} → ${r.status} ${await r.text()}`);
return r.json();
}
const companies = await db.companies.find({ happierleadsWebsiteId: null }); // your own query
let ok = 0, failed = 0;
for (const c of companies) {
if (DRY_RUN) { console.log('would create', c.id, c.name); continue; }
try {
const { websiteId } = await hl('POST', '/admin/websites', { name: c.name, domain: c.domain || '' });
await db.companies.update(c.id, { happierleadsWebsiteId: websiteId }); // save first, then limits
await hl('PUT', `/admin/websites/set-limits/${websiteId}`, { credits: c.plan.leads, emailAccounts: 2 });
await hl('PUT', `/admin/websites/toggle-limits/${websiteId}`, c.isPaying
? { trialEnabled: false, activeWebsite: true, enableWebsiteLimits: true, refreshOnDate: c.renewsAt }
: { trialEnabled: true, trialExpireDate: c.trialEndsAt, enableWebsiteLimits: true });
ok += 1;
console.log('ok', c.id, websiteId);
} catch (e) {
failed += 1;
console.error('FAILED', c.id, e.message); // run again to retry
}
await new Promise((r) => setTimeout(r, 300)); // about 3 calls a second
}
console.log({ total: companies.length, ok, failed });- Run it with
--dry-runand check the list. - Run it on staging, open a few clients' screens.
- Run it on production and compare the summary with your client count.
Next: security checklist.