ApifyClient (apify_client v1.0.0)

Copy Markdown View Source

Elixir client for the Apify API.

Setup

Every call takes its connection settings as options: a prebuilt config: %ApifyClient.Config{} or individual ones such as token: "...". Without a token, requests are sent unauthenticated, which works only for public endpoints. Nothing is read from the environment.

The client doesn't read the application environment for these; build the configuration from your own:

defmodule MyApp.Apify do
  def config do
    ApifyClient.Config.new(token: Application.fetch_env!(:my_app, :apify_token))
  end
end

ApifyClient.Runs.get(run_id, config: MyApp.Apify.config())

See ApifyClient.Config for the settings and ApifyClient.Client for the options every call accepts.

Usage

The modules directly under ApifyClient cover the common tasks:

{:ok, run} =
  ApifyClient.Actors.call("apify/web-scraper", %{
    "startUrls" => [%{"url" => "https://example.com"}]
  })

if ApifyClient.Runs.succeeded?(run) do
  items = ApifyClient.Datasets.stream_items(run.default_dataset_id) |> Enum.to_list()
end

The whole API is available under ApifyClient.API, one module per resource, generated from Apify's OpenAPI description. Its functions are named after the URL path:

RequestFunction
GET /v2/actor-runs/{runId}ApifyClient.API.Runs.get/2
POST /v2/actor-runs/{runId}/abortApifyClient.API.Runs.abort/2
GET /v2/actors/{actorId}/runsApifyClient.API.Actors.Runs.list/2
PUT /v2/key-value-stores/{id}/records/{key}ApifyClient.API.KeyValueStores.Records.update/4
GET /v2/users/meApifyClient.API.Users.Me.get/1

Responses are decoded into the structs in ApifyClient.Schemas, with snake_case fields:

{:ok, %ApifyClient.Schemas.Run{status: "SUCCEEDED", default_dataset_id: id}} =
  ApifyClient.API.Runs.get(run_id)

Errors are {:error, %ApifyClient.Error{}}.