const apiKey = process.env.ANCHOR_API_KEY;
async function createSession() {
const res = await fetch('https://api.anchorbrowser.io/v1/sessions', {
method: 'POST',
headers: {
'anchor-api-key': apiKey,
'Content-Type': 'application/json',
},
body: JSON.stringify({}),
});
const json = await res.json();
if (!res.ok) {
throw new Error(`create session failed: ${res.status} ${JSON.stringify(json)}`);
}
return json.data.id;
}
const sessionId = await createSession();
console.log('session', sessionId);
const res = await fetch(`https://api.anchorbrowser.io/v1/tools/execute-code?sessionId=${sessionId}`, {
method: 'POST',
headers: {
'anchor-api-key': apiKey,
'Content-Type': 'application/json',
},
body: JSON.stringify({
code: "await page.goto('https://news.ycombinator.com'); const title = await page.title(); return { title };",
}),
});
const body = await res.json();
console.log(body);