The official Python SDK for building Apify Actors.
apify is the official SDK for building Apify Actors in Python. It handles the Actor lifecycle, storage access, platform events, Apify Proxy, pay-per-event charging, and more.
If you only need to consume the Apify API from Python (running Actors, reading datasets, managing storages) rather than building Actors, use the Apify API client for Python instead. It comes bundled with this SDK.
- Installation
- Quick start
- What are Actors?
- Features
- What you can build
- Usage examples
- Documentation
- Related projects
- Support and community
- Contributing
- License
The Apify SDK for Python requires Python 3.11 or higher. It is published on PyPI as the apify package and can be installed with pip:
pip install apifyor with uv:
uv add apifyTo use the Scrapy integration, install the scrapy extra:
pip install 'apify[scrapy]'An Actor is a Python program that runs inside the async with Actor: context. The context initializes the Actor when it starts and tears it down when it finishes. Here's a minimal Actor that reads its input and stores a result:
from apify import Actor
async def main() -> None:
async with Actor:
actor_input = await Actor.get_input()
Actor.log.info('Actor input: %s', actor_input)
await Actor.set_value('OUTPUT', 'Hello, world!')The quickest way to scaffold a full Actor project, with the .actor configuration, input schema, and Dockerfile already in place, is the Apify CLI:
-
Install the CLI:
npm install -g apify-cli
-
Create a new Actor from the Python "getting started" template:
apify create my-actor --template python-start
-
Run it locally:
cd my-actor apify run
To create, run, and deploy your first Actor step by step, see the Quick start guide.
Actors are serverless cloud programs that can do almost anything a human can do in a web browser. They range from small tasks, such as filling in forms or unsubscribing from online services, all the way up to scraping and processing vast numbers of web pages.
They run either locally or on the Apify platform, where you can run them at scale, monitor them, schedule them, or publish and monetize them. If you're new to Apify, learn what Apify is in the platform documentation.
- Run the full Actor lifecycle inside
async with Actor:, covering init, exit, failures, status messages, and reboots (Actor lifecycle). - Read Actor input validated against your input schema with
Actor.get_input()(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 (Actor events).
- Route requests through Apify Proxy with group selection, country targeting, and rotation (Proxy management).
- Start, call, abort, and metamorph 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).
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 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.
The examples below show two common setups, but the same async with Actor: pattern works with any stack. For more, see the guides.
Scrape pages with HTTPX and BeautifulSoup, using the Actor's request queue to track URLs:
from bs4 import BeautifulSoup
from httpx import AsyncClient
from apify import Actor
async def main() -> None:
async with Actor:
actor_input = await Actor.get_input() or {}
start_urls = actor_input.get('start_urls', [{'url': 'https://apify.com'}])
# Enqueue the start URLs into the default request queue.
request_queue = await Actor.open_request_queue()
for start_url in start_urls:
await request_queue.add_request(start_url['url'])
# Process the queue until it's empty.
while request := await request_queue.fetch_next_request():
Actor.log.info(f'Scraping {request.url} ...')
async with AsyncClient() as client:
response = await client.get(request.url)
soup = BeautifulSoup(response.content, 'html.parser')
# Push the extracted data to the default dataset.
await Actor.push_data({
'url': request.url,
'title': soup.title.string if soup.title else None,
})Scrape pages with Crawlee's PlaywrightCrawler, which handles queueing, concurrency, and the browser for you:
from crawlee.crawlers import PlaywrightCrawler, PlaywrightCrawlingContext
from apify import Actor
async def main() -> None:
async with Actor:
actor_input = await Actor.get_input() or {}
start_urls = [url['url'] for url in actor_input.get('start_urls', [{'url': 'https://apify.com'}])]
crawler = PlaywrightCrawler(max_requests_per_crawl=50, headless=True)
@crawler.router.default_handler
async def handler(context: PlaywrightCrawlingContext) -> None:
Actor.log.info(f'Scraping {context.request.url} ...')
await context.push_data({
'url': context.request.url,
'title': await context.page.title(),
})
# Follow links found on the page.
await context.enqueue_links()
await crawler.run(start_urls)The full SDK documentation lives at docs.apify.com/sdk/python. For the Apify platform itself, see the Apify documentation.
| Section | What you'll find |
|---|---|
| Overview | What the SDK is, what Actors are, and how the pieces fit together. |
| Quick start | Create, run, and deploy your first Python Actor. |
| Concepts | Actor lifecycle, input, storages, events, proxy management, interacting with other Actors, webhooks, accessing the Apify API, logging, configuration, and pay-per-event. |
| Guides | Integrations with BeautifulSoup, Parsel, Playwright, Selenium, Crawlee, Scrapy, Crawl4AI, and Browser Use, plus running a web server and using uv. |
| Upgrading | Migrating between major versions. |
| API reference | Generated reference for every class and method. |
| Changelog | Release history and breaking changes. |
- Apify API client for Python — talk to the Apify API directly from Python (bundled with this SDK).
- Crawlee for Python — web scraping and browser automation framework; fully compatible with this SDK.
- Apify SDK for JavaScript / TypeScript — the equivalent SDK for Node.js.
- Apify API client for JavaScript / TypeScript — the equivalent API client for Node.js.
- Crawlee for JavaScript / TypeScript — the original Node.js implementation of Crawlee.
- Apify CLI — command-line tool for creating, running, and deploying Actors locally and on the platform.
- Discord — chat with the team and other users on the Apify Discord server.
- GitHub issues — report a bug or request a feature in the issue tracker.
Bug reports, fixes, and improvements are welcome! See CONTRIBUTING.md for the development setup, coding standards, testing, and release process. The project uses uv for project management and Poe the Poet as a task runner; the typical loop is:
uv run poe install-dev # install dev dependencies and git hooks
uv run poe check-code # lint, type-check, and unit testsReleased under the Apache License 2.0.