Recipes/Node fetch

Node fetch

Submit, poll, persist. No dependencies beyond the runtime.

The code

const API = "https://api.tubeextract.dev/v1";
const headers = {
"X-API-Key": process.env.TUBEEXTRACT_KEY,
"Content-Type": "application/json"
};
 
const schema = {
multiple: true,
columns: [
{ name: "restaurant_name",
description: "Name of the restaurant the host visited" },
{ name: "food_ordered",
description: "What the host said he ate there" }
]
};
 
const created = await fetch(`${API}/jobs`, {
method: "POST", headers,
body: JSON.stringify({ youtube_url: url, schema })
}).then(r => r.json());
 
while (true) {
await new Promise(r => setTimeout(r, 1500));
 
const job = await fetch(`${API}/${created.job_id}`,
{ headers }).then(r => r.json());
 
// null until every video has settled, so polling costs nothing
if (job.results !== null) {
await save(job.results); // persist before anything else
break;
}
}

Notes

The save call sits inside the loop deliberately. The rows are handed over once, so writing them has to happen in the same turn as reading them. Add a poll ceiling in production so a stuck job cannot spin forever.

Was this page useful?Tell us what was missing