Skip to main content

Running a webserver in your Actor

Each Actor run on the Apify platform is assigned a unique hard-to-guess URL (for example, which enables HTTP access to an optional web server running inside the Actor run's container.

The URL is available in the following places:

  • In Apify Console, 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 as the Actor.config.container_url property.

The web server running inside the container must listen at the port defined by the Actor.config.container_port property. When running Actors locally, the port defaults to 4321, so the web server will be accessible at http://localhost:4321.


The following example demonstrates how to start a simple web server in your Actor, which will respond to every GET request with the number of items that the Actor has processed so far:

import asyncio
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer

from apify import Actor

processed_items = 0
http_server = None

# Just a simple handler that will print the number of processed items so far
# on every GET request
class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.wfile.write(bytes(f'Processed items: {processed_items}', encoding='utf-8'))

def run_server():
# Start the HTTP server on the provided port,
# and save a reference to the server
global http_server
with ThreadingHTTPServer(('', Actor.config.container_port), RequestHandler) as server:'Server running on {Actor.config.container_url}')
http_server = server

async def main():
global processed_items
async with Actor:
# Start the HTTP server in a separate thread
run_server_task = asyncio.get_running_loop().run_in_executor(None, run_server)

# Simulate doing some work
for _ in range(100):
await asyncio.sleep(1)
processed_items += 1'Processed items: {processed_items}')

# Signal the HTTP server to shut down, and wait for it to finish
await run_server_task