> Documentation index: https://www.socialcrawl.dev/llms.txt > Every page is also available as Markdown by adding .md. # NestJS (/docs/quickstart/nestjs) Wrap SocialCrawl in a NestJS service and controller that read a profile, search 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. - NestJS 12. - 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 Nest app (`nest new` makes one), install the Nest packages, then set your key in the shell. ```bash title="Terminal" npm install @nestjs/common @nestjs/core @nestjs/platform-express reflect-metadata rxjs export SOCIALCRAWL_API_KEY=sc_your_key_here ``` Keep the key on the server, out of your code and out of the browser. ## Create a SocialCrawl service This code sends your key in the `x-api-key` header. When `success` is false it throws an `HttpException` holding the API's JSON, so the route answers with it unchanged. ```typescript title="src/socialcrawl/socialcrawl.service.ts" import { HttpException, Injectable, Logger } from "@nestjs/common"; const API = "https://www.socialcrawl.dev"; @Injectable() export class SocialCrawlService { private readonly key = process.env.SOCIALCRAWL_API_KEY ?? ""; private readonly logger = new Logger(SocialCrawlService.name); profile(handle: string) { return this.get("/v1/tiktok/profile", { handle }); } // dry_run=1 prices the search and runs nothing. search(query: string) { return this.get("/v1/search/everywhere", { query, format: "compact", dry_run: "1" }); } // Send the last answer's pagination.next_cursor back as cursor. posts(handle: string, cursor?: string) { return this.get("/v1/tiktok/profile/videos", cursor ? { handle, cursor } : { handle }); } // One request with your key. When success is false, log error.type and // error.doc_url, then answer with the API's JSON and status as they came. private async get(path: string, params: Record) { const res = await fetch(`${API}${path}?${new URLSearchParams(params)}`, { headers: { "x-api-key": this.key }, }); const body = (await res.json()) as { success: boolean; error?: { type: string; doc_url?: string } }; if (!body.success) { this.logger.warn(`${body.error?.type} (${body.error?.doc_url})`); throw new HttpException(body, res.status); } return body; } } ``` Branch on `success`, then on `error.type`, never on the message text. ## Create a controller Each of the three routes under `/api` makes one call and answers with what comes back. ```typescript title="src/socialcrawl/socialcrawl.controller.ts" import { Controller, Get, Query } from "@nestjs/common"; import { SocialCrawlService } from "./socialcrawl.service"; @Controller("api") export class SocialCrawlController { constructor(private readonly socialcrawl: SocialCrawlService) {} @Get("profile") profile(@Query("handle") handle = "charlidamelio") { return this.socialcrawl.profile(handle); } @Get("search") search(@Query("query") query = "oat milk") { return this.socialcrawl.search(query); } @Get("posts") posts(@Query("handle") handle = "charlidamelio", @Query("cursor") cursor?: string) { return this.socialcrawl.posts(handle, cursor); } } ``` With `dry_run=1` the search is free. Drop it to run the search for 20 credits. A page of posts costs 1 credit. Send `pagination.next_cursor` back as `cursor`, and stop when `pagination.has_more` is false. ## Register the module Wire the routes into the app. A module holds the controller and the service, and `AppModule` imports it. ```typescript title="src/socialcrawl/socialcrawl.module.ts" import { Module } from "@nestjs/common"; import { SocialCrawlController } from "./socialcrawl.controller"; import { SocialCrawlService } from "./socialcrawl.service"; @Module({ controllers: [SocialCrawlController], providers: [SocialCrawlService] }) export class SocialCrawlModule {} ``` ```typescript title="src/app.module.ts" import { Module } from "@nestjs/common"; import { SocialCrawlModule } from "./socialcrawl/socialcrawl.module"; @Module({ imports: [SocialCrawlModule] }) export class AppModule {} ``` ## Test it Start the app with `npm run start`, 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.