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:
ApifyClient.Actors- start Actors, optionally waiting for the run; build themApifyClient.Tasks- start saved Actor tasks, optionally waiting for the runApifyClient.Runs- get, wait for and abort runsApifyClient.Builds- get, wait for and abort buildsApifyClient.Datasets- list, stream and push dataset itemsApifyClient.KeyValueStores- get, set, delete and stream records and keysApifyClient.Logs- get and stream run and build logsApifyClient.Pagination- stream every item of any paginated list endpoint
{: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()
endThe 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:
| Request | Function |
|---|---|
GET /v2/actor-runs/{runId} | ApifyClient.API.Runs.get/2 |
POST /v2/actor-runs/{runId}/abort | ApifyClient.API.Runs.abort/2 |
GET /v2/actors/{actorId}/runs | ApifyClient.API.Actors.Runs.list/2 |
PUT /v2/key-value-stores/{id}/records/{key} | ApifyClient.API.KeyValueStores.Records.update/4 |
GET /v2/users/me | ApifyClient.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{}}.