Skip to main content

Container web server

Learn about how to run a web server inside your Actor to enable communication with the outside world through both UI and API.


Each Actor run is assigned a unique URL (e.g. kmdo7wpzlshygi.runs.apify.net) that allows HTTP access to an optional web server running inside the Actor's Docker container. This feature enhances your Actor's capabilities by enabling external communication.

Using Actors as an API

The container web server provides a way how to connect to one specific Actor run. To enable using your Actor as an API, with a pre-defined hostname, load balancing and autoscaling, check out Actor Standby.

Access the container URL

You can find the container URL in three locations:

  • In the web application, on the Actor run details page as the Container URL field.
  • In the API as the containerUrl property of the Run object.
  • In the Actor run's container as the ACTOR_WEB_SERVER_URL environment variable.

Set up the web server

The web server inside the container must listen on the port specified by the ACTOR_WEB_SERVER_PORT environment variable (typically: 4321). To use a different port:

  1. Go to your Actor version configuration

  2. Define the ACTOR_WEB_SERVER_PORT environment variable with your desired port number.

Check out Custom environment variables for more details.

Example: Start a simple web server

Here's how to start a basic web server in your Actor using Express.js:

// npm install express
import { Actor } from 'apify';
import express from 'express';

await Actor.init();

const app = express();
const port = process.env.ACTOR_WEB_SERVER_PORT;

app.get('/', (req, res) => {
res.send('Hello world from Express app!');
});

app.listen(port, () => console.log(`Web server is listening
and can be accessed at
${process.env.ACTOR_WEB_SERVER_URL}!`));

// Let the Actor run for an hour
await new Promise((r) => setTimeout(r, 60 * 60 * 1000));

await Actor.exit();