Skip to main content
Version: 3.4

Overview

The Apify SDK for Python is the official library for creating Apify Actors in Python. It provides everything you need to build an Actor and run it both locally and on the Apify platform. It handles the Actor lifecycle, storage access, platform events, Apify Proxy, pay-per-event charging, and more.

import asyncio

import httpx
from bs4 import BeautifulSoup

from apify import Actor


async def main() -> None:
async with Actor:
actor_input = await Actor.get_input() or {}
url = actor_input.get('url', 'https://apify.com')
async with httpx.AsyncClient() as client:
response = await client.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
data = {
'url': url,
'title': soup.title.string if soup.title else None,
}
await Actor.push_data(data)


if __name__ == '__main__':
asyncio.run(main())

What are Actors?

Actors are serverless programs that can do almost anything. From simple scripts and web scrapers to complex automation workflows, AI agents, or even always-on services that expose HTTP endpoints.

They can run either locally or on the Apify platform, where you can scale their execution, monitor runs, schedule tasks, integrate them with other services, or even publish and monetize them. If you're new to Apify, learn more about the platform in the Apify documentation.

For more context, read the Actor whitepaper.

Features

  • Run the full Actor lifecycle inside async with Actor:, covering init, exit, failures, status messages, rebooting, and metamorphing (Actor lifecycle).
  • Read Actor input validated against your input schema with Actor.get_input(), including automatic decryption of secret fields (Actor input).
  • Read and write datasets, key-value stores, and request queues, locally or on the platform (Working with storages).
  • React to platform events such as system info, migration, and abort, and persist state across migrations and restarts (Actor events).
  • Route requests through Apify Proxy with group selection, country targeting, and rotation, with session and tiered-proxy support (Proxy management).
  • Start, call, and abort other Actors and tasks, and attach webhooks to run events (Interacting with other Actors, Webhooks).
  • Monetize your Actor with pay-per-event charging (Pay-per-event).
  • Reach the full Apify API through a preconfigured ApifyClient (Accessing the Apify API).

What you can build

Almost any Python project can become an Actor, including projects for:

  • Web scraping and crawling - The SDK is fully compatible with Crawlee, which makes Apify a natural place to deploy and scale your crawlers (see the Crawlee guide). It also works with other popular scraping libraries, such as Scrapy, Scrapling, or Crawl4AI.
  • Browser automation - Drive a real browser with Playwright or Selenium, or with higher-level tools such as Browser Use.
  • Web servers and APIs - Run a web server inside an Actor to serve HTTP requests, for example to expose your scraper as a live API.
  • AI agents - Host agents built with your framework of choice. Ready-made Actor templates cover PydanticAI, CrewAI, LangGraph, LlamaIndex, and Smolagents.
  • MCP servers - Deploy a Python MCP server as an Actor and make its tools available to any MCP client. See the MCP server and MCP proxy templates.

Whatever you build, the Apify SDK doesn't lock you into a particular framework. Bring the libraries you already use, and let Apify run your project in the cloud.

Quick start

To create and run Actors using Apify Console, check out Apify Console documentation. For creating and running Python Actors locally, refer to the quick start guide.

Explore the Guides section in the sidebar for a deeper understanding of the SDK's features and best practices.

Installation

The Apify SDK for Python requires Python version 3.10 or above. It is typically installed when you create a new Actor project using the Apify CLI. To install it manually in an existing project, use:

pip install apify
API client alternative

If you need to interact with the Apify API programmatically without creating Actors, use the Apify API client for Python instead.