Skip to content

Commit da15ae7

Browse files
feat: add apify api subcommand for direct API access (#1076)
## Summary Adds `apify api` — a general-purpose authenticated HTTP client for the Apify API, inspired by `gh api`. - `apify api v2/users/me` — make authenticated GET requests - `apify api v2/acts --method POST --body '{...}'` — create resources - `apify api --list-endpoints` — browse all 129 API endpoints (from bundled OpenAPI spec) ### Features - HTTP methods via `--method` / `-X` (GET, POST, PUT, PATCH, DELETE) - Request body via `--body` / `-d` (JSON string or stdin with `-`) - Custom headers via `--header` / `-H` - `--list-endpoints` / `-l` — lists all available endpoints with color-coded methods and descriptions, powered by the Apify OpenAPI spec fetched at build time Closes #1075
1 parent 1a6cb26 commit da15ae7

6 files changed

Lines changed: 800 additions & 0 deletions

File tree

docs/reference.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,52 @@ FLAGS
4444
not provided, the latest version will be used.
4545
```
4646

47+
##### `apify api`
48+
49+
```sh
50+
DESCRIPTION
51+
Makes an authenticated HTTP request to the Apify API and prints the response.
52+
The endpoint can be a relative path (e.g. "acts", "v2/acts", or "/v2/acts").
53+
The "v2/" prefix is added automatically if omitted.
54+
55+
You can also pass the HTTP method before the endpoint:
56+
apify api GET /v2/actor-runs
57+
apify api POST /v2/acts -d '{"name": "my-actor"}'
58+
59+
Use --params/-p to pass query parameters as JSON:
60+
apify api actor-runs -p '{"limit": 1, "desc": true}'
61+
62+
Use --list-endpoints to see all available API endpoints.
63+
For full documentation, see https://docs.apify.com/api/v2
64+
65+
USAGE
66+
$ apify api [methodOrEndpoint] [endpoint] [-d <value>] [-H <value>]
67+
[-l] [-X GET|POST|PUT|PATCH|DELETE] [-p <value>]
68+
69+
ARGUMENTS
70+
methodOrEndpoint The API endpoint path (e.g. "acts",
71+
"v2/acts", "/v2/users/me"), or an HTTP method followed by the
72+
endpoint (e.g. "GET /v2/users/me").
73+
endpoint The API endpoint path when the first
74+
argument is an HTTP method.
75+
76+
FLAGS
77+
-d, --body=<value> The request body (JSON string). Use
78+
"-" to read from stdin.
79+
-H, --header=<value> Additional HTTP header(s). Pass a
80+
single "key:value" string, or a JSON object like '{"X-Foo":
81+
"bar", "X-Baz": "qux"}' to send multiple headers. The flag
82+
can only be used once; use the JSON form for multiple
83+
headers.
84+
-l, --list-endpoints List all available Apify API
85+
endpoints.
86+
-X, --method=<option> The HTTP method to use. Defaults to
87+
GET.
88+
<options: GET|POST|PUT|PATCH|DELETE>
89+
-p, --params=<value> Query parameters as a JSON object,
90+
e.g. '{"limit": 1, "desc": true}'.
91+
```
92+
4793
##### `apify telemetry`
4894
4995
```sh

scripts/generate-cli-docs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ const categories: Record<string, CommandsInCategory[]> = {
7575
//
7676
{ command: Commands.help },
7777
{ command: Commands.upgrade },
78+
{ command: Commands.api },
7879
{ command: Commands.telemetry },
7980
{ command: Commands.telemetryEnable },
8081
{ command: Commands.telemetryDisable },

src/commands/_register.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ActorGetValueCommand } from './actor/get-value.js';
99
import { ActorPushDataCommand } from './actor/push-data.js';
1010
import { ActorSetValueCommand } from './actor/set-value.js';
1111
import { ActorsIndexCommand } from './actors/_index.js';
12+
import { ApiCommand } from './api.js';
1213
import { AuthIndexCommand } from './auth/_index.js';
1314
import { BuildsIndexCommand } from './builds/_index.js';
1415
import { TopLevelCallCommand } from './call.js';
@@ -49,6 +50,7 @@ export const apifyCommands = [
4950
TelemetryIndexCommand,
5051

5152
// top-level
53+
ApiCommand,
5254
TopLevelCallCommand,
5355
UpgradeCommand,
5456
InstallCommand,

0 commit comments

Comments
 (0)