Frameworks
SocialCrawl with 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_.
계정을 확인하는 중입니다.
Set up
In your Nest app (nest new makes one), install the Nest packages, then set your key in the shell.
npm install @nestjs/common @nestjs/core @nestjs/platform-express reflect-metadata rxjs
export SOCIALCRAWL_API_KEY=sc_your_key_hereKeep 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.
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<string, string>) {
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.
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.
import { Module } from "@nestjs/common";
import { SocialCrawlController } from "./socialcrawl.controller";
import { SocialCrawlService } from "./socialcrawl.service";
@Module({ controllers: [SocialCrawlController], providers: [SocialCrawlService] })
export class SocialCrawlModule {}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.
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="