> Documentation index: https://www.socialcrawl.dev/llms.txt > Every page is also available as Markdown by adding .md. # Next.js (/docs/quickstart/nextjs) Read a profile, search many platforms and page through posts from Next.js route handlers and server actions The app serves three routes under `/api`, and each answers with the API's JSON unchanged. ## Prerequisites - Node.js 22 or later. - Next.js 16 with the App Router. - 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 In your Next.js app, add `server-only` and put your key in `.env.local`. ```bash title="Terminal" npm install server-only echo "SOCIALCRAWL_API_KEY=sc_your_key_here" >> .env.local ``` **Warning.** Never name the variable `NEXT_PUBLIC_SOCIALCRAWL_API_KEY`. Next.js copies every `NEXT_PUBLIC_` value into the browser bundle, where anyone can read the key. ## Get a profile The helper sends your key in the `x-api-key` header and answers with the API's JSON and status. `server-only` fails the build if a client component imports it. A Server Action cannot be called with curl, so Test it uses the route. ```typescript title="lib/socialcrawl.ts" import "server-only"; const API = "https://www.socialcrawl.dev"; // Send one request with your key. The answer carries success, and // error.type and error.doc_url when success is false. export async function socialcrawl(path: string, params: Record) { const res = await fetch(`${API}${path}?${new URLSearchParams(params)}`, { headers: { "x-api-key": process.env.SOCIALCRAWL_API_KEY ?? "" }, }); return { status: res.status, body: await res.json() }; } ``` **Route Handler** ```typescript title="app/api/profile/route.ts" import type { NextRequest } from "next/server"; import { socialcrawl } from "@/lib/socialcrawl"; export async function GET(request: NextRequest) { const handle = request.nextUrl.searchParams.get("handle") ?? "charlidamelio"; const { status, body } = await socialcrawl("/v1/tiktok/profile", { handle }); return Response.json(body, { status }); } ``` **Server Action** ```typescript title="app/actions.ts" "use server"; import { socialcrawl } from "@/lib/socialcrawl"; export async function getFollowers(handle: string) { const { body } = await socialcrawl("/v1/tiktok/profile", { handle }); if (!body.success) throw new Error(`${body.error.type} (${body.error.doc_url})`); return body.data.author.followers as number; } ``` 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. ```typescript title="app/api/search/route.ts" import type { NextRequest } from "next/server"; import { socialcrawl } from "@/lib/socialcrawl"; export async function GET(request: NextRequest) { const query = request.nextUrl.searchParams.get("query") ?? "oat milk"; // dry_run=1 prices the search and runs nothing. const { status, body } = await socialcrawl("/v1/search/everywhere", { query, format: "compact", dry_run: "1", }); return Response.json(body, { status }); } ``` 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. ```typescript title="app/api/posts/route.ts" import type { NextRequest } from "next/server"; import { socialcrawl } from "@/lib/socialcrawl"; export async function GET(request: NextRequest) { const { searchParams } = request.nextUrl; const params: Record = { handle: searchParams.get("handle") ?? "charlidamelio", }; // Send the last answer's pagination.next_cursor back as ?cursor=. const cursor = searchParams.get("cursor"); if (cursor) params.cursor = cursor; const { status, body } = await socialcrawl("/v1/tiktok/profile/videos", params); return Response.json(body, { status }); } ``` A page of posts costs 1 credit. ## Test it Start the app with `npm run dev`, 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.