Each example below is a complete, runnable scenario. The canonical, compiled versions live in the
client's source repository under
tests/Apify.Client.Tests/Examples
— a repo-internal path that is not part of the published NuGet package — and are executed end-to-end against the live API by the
Test examples CI step (they require an APIFY_TOKEN), so the snippets here are guaranteed to stay
valid and working.
Every snippet runs inside an async context and assumes the following using directives appear at the
top of the file, before any top-level statements (a using after the first statement is a CS1529
compile error). ImplicitUsings is disabled in this repository, so even System is listed explicitly:
using System;
using System.Text;
using Apify.Client;
using Apify.Client.Models;
using Apify.Client.Options;
var client = new ApifyClient(
Environment.GetEnvironmentVariable("APIFY_TOKEN")
?? throw new InvalidOperationException("Set the APIFY_TOKEN environment variable."));// The third argument bounds the wait in seconds (120 here); pass null to wait indefinitely.
var run = await client.Actor("apify/hello-world").CallAsync(null, null, 120);
var items = await client.Dataset(run.DefaultDatasetId!).ListItemsAsync(new DatasetListItemsOptions());
// Count is the number of items in THIS page; Total is the dataset's full count across all pages.
Console.WriteLine($"Items on this page: {items.Count} (of {items.Total} total)");// Dataset
var dataset = await client.Datasets().GetOrCreateAsync("example-ds");
await client.Dataset(dataset.Id!).PushItemsAsync(new[] { new { hello = "world" } });
var items = await client.Dataset(dataset.Id!).ListItemsAsync(new DatasetListItemsOptions());
Console.WriteLine("Dataset items: " + items.Count);
// Key-value store
var store = await client.KeyValueStores().GetOrCreateAsync("example-kvs");
await client.KeyValueStore(store.Id!).SetRecordJsonAsync("OUTPUT", new { answer = 42 });
var record = await client.KeyValueStore(store.Id!).GetRecordAsync("OUTPUT");
// GetRecordAsync returns a record whose .Value is the raw bytes; decode JSON/text with UTF-8.
var recordText = record is null ? string.Empty : Encoding.UTF8.GetString(record.Value);
Console.WriteLine("KVS record: " + recordText);
// Request queue
var queue = await client.RequestQueues().GetOrCreateAsync("example-rq");
await client.RequestQueue(queue.Id!).AddRequestAsync(new RequestQueueRequest("https://example.com", "example"));
var head = await client.RequestQueue(queue.Id!).ListHeadAsync(10);
Console.WriteLine("Queue head size: " + head.Items.Count);var user = await client.Me().GetAsync();
if (user is not null)
{
Console.WriteLine("Account " + user.Id + " / " + user.Username);
}var created = await client.Actors().CreateAsync(new
{
name = "example-actor",
isPublic = false,
versions = new[]
{
new
{
versionNumber = "0.0",
sourceType = "SOURCE_FILES",
buildTag = "latest",
sourceFiles = new object[]
{
new { name = "Dockerfile", format = "TEXT", content = "FROM apify/actor-node:20\nCOPY . ./\nCMD node main.js" },
new { name = "main.js", format = "TEXT", content = "console.log('hi');" },
},
},
},
});
var build = await client.Actor(created.Id!).BuildAsync("0.0", new ActorBuildOptions());
await client.Build(build.Id!).WaitForFinishAsync(300);
var run = await client.Actor(created.Id!).CallAsync(null, null, 120);
var log = await client.Run(run.Id!).Log().GetAsync();
Console.WriteLine(log);await client.Actor("apify/hello-world").CallAsync(null, null, 120);
var last = await client.Actor("apify/hello-world").LastRun(new LastRunOptions { Status = "SUCCEEDED" }).GetAsync();
if (last is not null)
{
// Read all three of the run's default storages: dataset, key-value store, request queue.
await client.Dataset(last.DefaultDatasetId!).ListItemsAsync(new DatasetListItemsOptions());
await client.KeyValueStore(last.DefaultKeyValueStoreId!).GetRecordAsync("OUTPUT");
await client.RequestQueue(last.DefaultRequestQueueId!).GetAsync();
Console.WriteLine("Last run: " + last.Id);
}var shown = 0;
await foreach (var item in client.Store().IterateAsync(new StoreListOptions { Limit = 10 }))
{
Console.WriteLine(item.Name);
if (++shown >= 5)
{
break;
}
}// The `log` argument redirects the run's live log to the given sink (here stdout) while it runs; the
// client streams the log and forwards each complete message as it arrives.
await client.Actor("apify/hello-world").CallAsync(null, null, 120, log: Console.WriteLine);You can also redirect a specific run's log yourself with GetStreamedLog:
var run = await client.Actor("apify/hello-world").StartAsync();
await using var streamedLog = client.Run(run.Id!).GetStreamedLog(Console.WriteLine);
streamedLog.Start();
await client.Run(run.Id!).WaitForFinishAsync(120);
await streamedLog.StopAsync();