Back to template gallery

TypeScript API Actor

An Actor that stays running, ready to respond to HTTP requests. Good for APIs and real-time services.

Language

typescript

Tools

nodejs

Use cases

Starter

Integration

src/main.ts

1import http from 'node:http';
2
3// Apify SDK - toolkit for building Apify Actors (Read more at https://docs.apify.com/sdk/js/)
4import { Actor } from 'apify';
5
6// this is ESM project, and as such, it requires you to specify extensions in your relative imports
7// read more about this here: https://nodejs.org/docs/latest-v18.x/api/esm.html#mandatory-file-extensions
8// note that we need to use `.js` even when inside TS files
9// import { router } from './routes.js';
10
11// The init() call configures the Actor to correctly work with the Apify-provided environment - mainly the storage infrastructure. It is necessary that every Actor performs an init() call.
12await Actor.init();
13
14// Create a simple HTTP server that will respond with a message
15const server = http.createServer((req, res) => {
16 // Handle Apify standby readiness probe
17 // https://docs.apify.com/platform/actors/development/programming-interface/standby#readiness-probe
18 if (req.headers['x-apify-container-server-readiness-probe']) {
19 res.writeHead(200);
20 res.end('ok');
21 return;
22 }
23 res.writeHead(200, { 'Content-Type': 'text/plain' });
24 res.end('Hello from Actor Standby!\n');
25});
26
27// Listen on the standby port
28server.listen(Actor.config.get('standbyPort'));

Standby TypeScript template

Start a new web scraping  project quickly and easily in TypeScript (Node.js) with our Standby project template. It provides a basic structure for building an Actor with Apify SDK  and allows you to easily add your own functionality.

Included features

Resources

Already have a solution in mind?

Sign up for a free Apify account and deploy your code to the platform in just a few minutes! If you want a head start without coding it yourself, browse our Store of existing solutions.