> Documentation index: https://www.socialcrawl.dev/llms.txt > Every page is also available as Markdown by adding .md. # AWS Lambda (/docs/quickstart/aws-lambda) Deploy a Lambda function that reads a profile, searches many platforms and pages through posts One handler answers three routes, `/api/profile`, `/api/search` and `/api/posts`, with the API's JSON unchanged. ## Prerequisites - An AWS account, the AWS CLI, and an execution role for the function. - The Node.js 22 Lambda runtime. - 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 The function reads the key from its environment. For production, keep it in AWS Secrets Manager instead. ```bash title="Terminal" export SOCIALCRAWL_API_KEY=sc_your_key_here export LAMBDA_ROLE_ARN=arn:aws:iam::123456789012:role/socialcrawl-lambda ``` **Warning.** Do not give the function a public Function URL. Anyone who finds an open URL can spend your credits. Call it with `aws lambda invoke`, or put it behind an authorizer. ## Get a profile The helper sends your key in the `x-api-key` header and answers with the API's JSON and status. `handler` reads the route from `event.rawPath` and the query from `event.queryStringParameters`. ```javascript title="index.mjs" const API = "https://www.socialcrawl.dev"; // Send one request with your key and answer with the API's JSON and status. // The JSON carries success, and error.type and error.doc_url when it is false. async function socialcrawl(path, params) { const res = await fetch(`${API}${path}?${new URLSearchParams(params)}`, { headers: { "x-api-key": process.env.SOCIALCRAWL_API_KEY }, }); return { statusCode: res.status, headers: { "content-type": "application/json" }, body: await res.text(), }; } function profile(query) { return socialcrawl("/v1/tiktok/profile", { handle: 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="index.mjs" // dry_run=1 prices the search and runs nothing. function search(query) { return socialcrawl("/v1/search/everywhere", { query: 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="index.mjs" // Send the last answer's pagination.next_cursor back as cursor. function posts(query) { const params = { handle: query.handle ?? "charlidamelio" }; if (query.cursor) params.cursor = query.cursor; return socialcrawl("/v1/tiktok/profile/videos", params); } const routes = { "/api/profile": profile, "/api/search": search, "/api/posts": posts }; // An API Gateway (v2) or Function URL event: the path and the query. export const handler = async (event) => { const route = routes[event.rawPath]; if (!route) return { statusCode: 404, body: "Not found" }; return route(event.queryStringParameters ?? {}); }; ``` A page of posts costs 1 credit. ## Deploy Zip `index.mjs` and create the function with the key in its environment. After a change, run `aws lambda update-function-code` with the same zip. ```bash title="Terminal" zip function.zip index.mjs aws lambda create-function --function-name socialcrawl --runtime nodejs22.x --handler index.handler --role "$LAMBDA_ROLE_ARN" --zip-file fileb://function.zip --environment "Variables={SOCIALCRAWL_API_KEY=$SOCIALCRAWL_API_KEY}" ``` ## Test it Invoke the function once per route. Each file holds the answer, and its `body` holds the API's JSON with `data.author.followers`, `data.estimate` or `pagination.next_cursor`. ```bash title="Terminal" aws lambda invoke --function-name socialcrawl --cli-binary-format raw-in-base64-out --payload '{"rawPath":"/api/profile","queryStringParameters":{"handle":"charlidamelio"}}' profile.json aws lambda invoke --function-name socialcrawl --cli-binary-format raw-in-base64-out --payload '{"rawPath":"/api/search","queryStringParameters":{"query":"oat milk"}}' search.json aws lambda invoke --function-name socialcrawl --cli-binary-format raw-in-base64-out --payload '{"rawPath":"/api/posts","queryStringParameters":{"handle":"charlidamelio","cursor":""}}' posts.json ``` ## 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.