Frameworks
SocialCrawl with SvelteKit
Read a profile, search many platforms and page through posts from SvelteKit endpoints and load functions
The app serves three routes under /api, and each answers with the API's JSON unchanged.
Prerequisites
- Node.js 22 or later.
- SvelteKit 3.
- A SocialCrawl API key. Keys start with
sc_.
계정을 확인하는 중입니다.
Set up
There is no package to install. Put your key in .env, and declare it in src/env.ts so the helper can import it from $app/env/private.
echo "SOCIALCRAWL_API_KEY=sc_your_key_here" >> .envNever mark the key public: true or import it from $app/env/public. SvelteKit sends public variables to the browser.
import { defineEnvVars } from "@sveltejs/kit/env";
// Private, the default: $app/env/private refuses to load in browser code.
export const variables = defineEnvVars({ SOCIALCRAWL_API_KEY: {} });Get a profile
The helper sends your key in the x-api-key header and answers with the API's JSON and status. SvelteKit refuses to bundle src/lib/server into browser code, so a slip fails the build.
import { SOCIALCRAWL_API_KEY } from "$app/env/private";
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<string, string>) {
const res = await fetch(`${API}${path}?${new URLSearchParams(params)}`, {
headers: { "x-api-key": SOCIALCRAWL_API_KEY },
});
return { status: res.status, body: await res.json() };
}import { json } from "@sveltejs/kit";
import { socialcrawl } from "#lib/server/socialcrawl.ts";
import type { RequestHandler } from "./$types";
export const GET: RequestHandler = async ({ url }) => {
const handle = url.searchParams.get("handle") ?? "charlidamelio";
const { status, body } = await socialcrawl("/v1/tiktok/profile", { handle });
return json(body, { status });
};import { error } from "@sveltejs/kit";
import { socialcrawl } from "#lib/server/socialcrawl.ts";
export async function load() {
const { status, body } = await socialcrawl("/v1/tiktok/profile", { handle: "charlidamelio" });
if (!body.success) error(status, `${body.error.type} (${body.error.doc_url})`);
return { followers: 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.
import { json } from "@sveltejs/kit";
import { socialcrawl } from "#lib/server/socialcrawl.ts";
import type { RequestHandler } from "./$types";
// dry_run=1 prices the search and runs nothing.
export const GET: RequestHandler = async ({ url }) => {
const query = url.searchParams.get("query") ?? "oat milk";
const { status, body } = await socialcrawl("/v1/search/everywhere", {
query,
format: "compact",
dry_run: "1",
});
return 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.
import { json } from "@sveltejs/kit";
import { socialcrawl } from "#lib/server/socialcrawl.ts";
import type { RequestHandler } from "./$types";
// Send the last answer's pagination.next_cursor back as ?cursor=.
export const GET: RequestHandler = async ({ url }) => {
const params: Record<string, string> = { handle: url.searchParams.get("handle") ?? "charlidamelio" };
const cursor = url.searchParams.get("cursor");
if (cursor) params.cursor = cursor;
const { status, body } = await socialcrawl("/v1/tiktok/profile/videos", params);
return 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.
curl "http://localhost:5173/api/profile?handle=charlidamelio"
curl "http://localhost:5173/api/search?query=oat+milk"
curl "http://localhost:5173/api/posts?handle=charlidamelio&cursor="