Writing a scraper
One .mjs file exports three things. The platform turns them into routes, docs and a playground without any other change.
The contract
export const meta = {
name: "AnimeSail",
slug: "animesail",
version: "v1",
description: "Catalogue, episodes and schedule from the source.",
source: "https://example.com",
tags: ["anime"],
};
export const endpoints = [
{
id: "search",
title: "Search",
description: "Search titles by keyword.",
method: "GET",
path: "/search",
cacheSeconds: 120,
params: [
{ name: "q", in: "query", required: true, type: "string",
description: "Keyword", example: "naruto", maxLength: 120 },
],
},
];
export const handlers = {
async search(ctx, input) {
const html = await ctx.text("https://example.com/?s=" + encodeURIComponent(input.q));
const titles = [...html.matchAll(/<h2 class="title">(.+?)<\/h2>/g)].map((match) => match[1]);
return { query: input.q, count: titles.length, titles };
},
};meta
name, slug and version are required and become the URL.description is shown on the docs page and the index.
endpoints
One entry per handler. path may contain path parameters written as :name. params drives both validation and the documentation table.cacheSeconds sets how long a successful answer is reused, 0 to 3600.
handlers
The key is the endpoint id. Each handler receives the context and an object of validated parameters, and returns JSON-serialisable data.
The context
| Member | What it does |
|---|---|
ctx.fetch(url, init) | A fetch that refuses private addresses, caps the body at 6 MB, follows three redirects and times out at 12 seconds. |
ctx.text(url) | The same call, already read as text, throwing on a non 2xx status. |
ctx.json(url, init) | The same call, parsed as JSON. |
ctx.log(...) | Writes to the function log with the scraper and endpoint prefixed. |
ctx.signal | Aborted when the request budget runs out. Pass it to your own work. |
What a scraper may do
- Use plain ESM syntax and export
meta,endpoints,handlers. - Import
cheeriooraxios. Any other import is rejected when the file is loaded. - Fetch public http and https hosts.
- Keep the response under 2 MB and finish inside 9 seconds.
What it may not do:
- Read environment variables, the filesystem or the network stack directly.
- Import another scraper or share state with one.
- Return a response larger than the cap, or hold the request open with a timer.
Registering it
From the dashboard
Admin, Scrapers, Add scraper. The file is checked by loading it in the sandbox before anything is saved; a module that throws, exports the wrong shape or imports a module that is not allowed is rejected with the reason. The source and the metadata go to Redis, and the endpoint is live without a redeploy.
From the repository
Put the file at scrapers/<slug>/<version>/index.mjs, add an entry to scrapers/manifest.json, and run npm run scrapers:sync. The import map is generated at build, so the file is bundled with the deployment and needs no storage.
Versioning
A breaking change means a new version: v2 beside v1. Both stay live, both keep their own docs page, and callers migrate on their own schedule.