Skip to content

apify/apify-sdk-python

Repository files navigation

Apify SDK for Python

The official Python SDK for building Apify Actors.

PyPI version PyPI downloads Python versions Build status Coverage License Chat on Discord

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.

Table of contents

Installation

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 apify

or with uv:

uv add apify

To use the Scrapy integration, install the scrapy extra:

pip install 'apify[scrapy]'

Quick start

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:

  1. Install the CLI:

    npm install -g apify-cli
  2. Create a new Actor from the Python "getting started" template:

    apify create my-actor --template python-start
  3. Run it locally:

    cd my-actor
    apify run

To create, run, and deploy your first Actor step by step, see the Quick start guide.

What are Actors?

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.

Features

  • 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).

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 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.

Usage examples

The examples below show two common setups, but the same async with Actor: pattern works with any stack. For more, see the guides.

HTTPX with BeautifulSoup

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,
            })

Crawlee with Playwright

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)

Documentation

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.

Related projects

Support and community

Contributing

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 tests

License

Released under the Apache License 2.0.

About

Apify SDK for Python—The official library for building Apify Actors: serverless cloud programs for web scraping, browser automation, data processing, and AI agents. Manages the Actor lifecycle, storages (datasets, key-value stores, request queues), events, proxies, and pay-per-event monetization. Built on top of the the Apify API Client.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Contributors