> Documentation index: https://www.socialcrawl.dev/llms.txt > Every page is also available as Markdown by adding .md. # Fastify (/docs/quickstart/fastify) Build a Fastify API with routes that read a profile, search many platforms and page through posts The app serves three routes under `/api`, and each answers with the API's JSON unchanged. ## Prerequisites - Node.js 22 or later. - Fastify 5. - A SocialCrawl API key. Keys start with `sc_`. Get a free API key at https://www.socialcrawl.dev/dashboard/api. New accounts start with 100 welcome credits and no card is needed. ## Set up Install Fastify and `fastify-plugin`, then set your key in the shell. ```bash title="Terminal" npm install fastify fastify-plugin export SOCIALCRAWL_API_KEY=sc_your_key_here ``` Keep the key on the server, out of your code and out of the browser. ## Get a profile The helper sends your key in the `x-api-key` header and answers with the API's JSON and status. It lives in a plugin, and every route calls it as `fastify.socialcrawl`. ```javascript title="plugins/socialcrawl.mjs" import fp from "fastify-plugin"; const API = "https://www.socialcrawl.dev"; // fastify.socialcrawl sends one request with your key and answers with the // API's JSON and status. The JSON carries success, and error.type and // error.doc_url when it is false. export default fp(async (fastify) => { fastify.decorate("socialcrawl", async (reply, path, params) => { const res = await fetch(`${API}${path}?${new URLSearchParams(params)}`, { headers: { "x-api-key": process.env.SOCIALCRAWL_API_KEY }, }); return reply.code(res.status).send(await res.json()); }); }); ``` ```javascript title="server.mjs" import Fastify from "fastify"; import socialcrawl from "./plugins/socialcrawl.mjs"; const fastify = Fastify(); await fastify.register(socialcrawl); fastify.get("/api/profile", (req, reply) => fastify.socialcrawl(reply, "/v1/tiktok/profile", { handle: req.query.handle ?? "charlidamelio", }), ); ``` Every answer carries `success`. Branch on it, then on `error.type`, never on the message text. ## Search across platforms With `dry_run=1` the search is free and runs nothing. `data.estimate` holds the credits and seconds the real search would take. ```javascript title="server.mjs" // dry_run=1 prices the search and runs nothing. fastify.get("/api/search", (req, reply) => fastify.socialcrawl(reply, "/v1/search/everywhere", { query: req.query.query ?? "oat milk", format: "compact", dry_run: "1", }), ); ``` Drop `dry_run` to run the search for 20 credits. ## Get posts and the next page Each answer holds one page of posts in `data.items`. Send `pagination.next_cursor` back as `cursor`, and stop when `pagination.has_more` is false. ```javascript title="server.mjs" // Send the last answer's pagination.next_cursor back as ?cursor=. fastify.get("/api/posts", (req, reply) => { const params = { handle: req.query.handle ?? "charlidamelio" }; if (req.query.cursor) params.cursor = req.query.cursor; return fastify.socialcrawl(reply, "/v1/tiktok/profile/videos", params); }); await fastify.listen({ port: Number(process.env.PORT ?? 3000) }); ``` A page of posts costs 1 credit. ## As a Fastify plugin Fastify gives each plugin its own scope, so a plain `fastify.decorate` stays inside it. `fastify-plugin` lifts the helper to the root, so every route, in any later plugin too, reads `fastify.socialcrawl`. ## Test it Start the app with `node server.mjs`, then call the three routes. The answers hold `data.author.followers`, `data.estimate` and `pagination.next_cursor`. ```bash title="Terminal" curl "http://localhost:3000/api/profile?handle=charlidamelio" curl "http://localhost:3000/api/search?query=oat+milk" curl "http://localhost:3000/api/posts?handle=charlidamelio&cursor=" ``` ## Next steps - [Price a watch for free](/docs/engines/monitors.md): POST /v1/monitors?dry_run=1 quotes a scheduled watch of an account and creates nothing. - [Common jobs](/docs/common-jobs.md): The first call for each job, from profiles to comments and reviews. - [Search](/docs/search.md): Choose between one platform - [Errors](/docs/errors.md): Every error type, what it means, and which ones to retry.