Skip to content

Commit 00acd44

Browse files
committed
Add veryfi CLI for shell and AI agent usage
Wraps every public Client method behind a Typer-based `veryfi` console script (and `python -m veryfi`) so AI agents and shell users can drive the SDK from a terminal. The CLI is purely additive: the Client API, constructor signature, and all existing tests are unchanged. Highlights: - One Typer sub-app per resource (documents, bank-statements, checks, business-cards, w2s, w8s, w9s, any-docs, classify) plus nested line-items, tags, and PDF/W-2 split sub-apps. - Env-var-first auth (VERYFI_CLIENT_ID, VERYFI_USERNAME, VERYFI_API_KEY, plus optional VERYFI_CLIENT_SECRET / VERYFI_BASE_URL / VERYFI_API_VERSION / VERYFI_TIMEOUT) with equivalent flags as overrides. - JSON output on stdout, structured JSON errors on stderr, exit codes mapped from the HTTP status (clipped to 1-255) so agents can branch without parsing tracebacks. - `veryfi schema` emits a machine-readable manifest of every command and parameter for tool registration. - Repeatable `--field KEY=VALUE` and `--json-body` for endpoints that take **kwargs; stdin support via `--file -`. - New tests/test_cli.py covers help discovery, env-var auth, happy paths per resource, error-to-exit-code mapping, schema shape, and output formats. Total suite: 62 tests passing (35 existing + 27 new), black clean. Also makes the User-Agent header dynamic via importlib.metadata so it stays in sync with the pbr-derived version without manual edits.
1 parent 68fdd50 commit 00acd44

18 files changed

Lines changed: 2638 additions & 1 deletion

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ tox = "*"
1414

1515
[packages]
1616
requests = "*"
17+
typer = ">=0.12.0"
1718

1819
[requires]
1920
python_version = "3.9"

README.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Extract structured data from receipts, invoices, bank statements, checks, W-2s,
2929
- [Any Document](#any-document)
3030
- [Classify](#classify)
3131
- [Error Handling](#error-handling)
32+
- [Command-line interface](#command-line-interface)
3233
- [Contributing](#contributing)
3334
- [Need Help?](#need-help)
3435
- [Changelog](#changelog)
@@ -366,6 +367,118 @@ except VeryfiClientError as e:
366367

367368
---
368369

370+
## Command-line interface
371+
372+
Installing `veryfi` also installs a `veryfi` console script (and the equivalent `python -m veryfi`). The CLI is a thin wrapper around the Python `Client` and exposes every supported resource as a sub-command — designed for shell users and AI agents that drive the SDK from a terminal.
373+
374+
Verify the install:
375+
376+
```bash
377+
veryfi --help
378+
# or, equivalently:
379+
python -m veryfi --help
380+
```
381+
382+
### Authentication
383+
384+
Credentials are read from environment variables (preferred for agents) or equivalent flags:
385+
386+
| Env var | Flag | Description |
387+
|---------|------|-------------|
388+
| `VERYFI_CLIENT_ID` | `--client-id` | Required |
389+
| `VERYFI_CLIENT_SECRET` | `--client-secret` | Optional — enables HMAC request signing |
390+
| `VERYFI_USERNAME` | `--username` | Required |
391+
| `VERYFI_API_KEY` | `--api-key` | Required |
392+
| `VERYFI_BASE_URL` | `--base-url` | Optional, defaults to `https://api.veryfi.com/api/` |
393+
| `VERYFI_API_VERSION` | `--api-version` | Optional, defaults to `v8` |
394+
| `VERYFI_TIMEOUT` | `--timeout` | Optional, defaults to `30` seconds |
395+
396+
If any required credential is missing the CLI exits with code `2` and a JSON error on stderr.
397+
398+
### Quick examples
399+
400+
```bash
401+
export VERYFI_CLIENT_ID=... VERYFI_USERNAME=... VERYFI_API_KEY=...
402+
# Optional:
403+
export VERYFI_CLIENT_SECRET=...
404+
405+
# Documents
406+
veryfi documents process --file /tmp/receipt.jpg --category Travel --category Meals
407+
veryfi documents process-url --file-url https://cdn.example.com/x.pdf --boost-mode --external-id ref-1
408+
veryfi documents list --q Walgreens --created-gt 2024-01-01+00:00:00
409+
veryfi documents get 933760836
410+
veryfi documents update 933760836 --field category="Meals & Entertainment" --field total=11.23
411+
veryfi documents delete 933760836
412+
413+
# Nested line-items / tags
414+
veryfi documents line-items add 933760836 --field description="Extra item" --field total=5.0
415+
veryfi documents tags add-many 933760836 --tag q1 --tag travel
416+
417+
# Multi-page PDF splitting
418+
veryfi documents set split --file /tmp/multi.pdf
419+
veryfi documents set split-url --file-url https://cdn.example.com/multi.pdf --max-pages 5
420+
421+
# Other resources
422+
veryfi bank-statements process --file /tmp/stmt.pdf --category Transfer
423+
veryfi checks process-with-remittance --file /tmp/check.pdf
424+
veryfi business-cards process-url --file-url https://cdn.example.com/card.jpg
425+
veryfi w2s process --file /tmp/w2.pdf
426+
veryfi w2s set split --file /tmp/multi_w2.pdf
427+
veryfi w8s list --created-gt 2024-01-01+00:00:00
428+
veryfi w9s get 33333
429+
veryfi any-docs process --blueprint my_blueprint --file /tmp/custom.pdf
430+
veryfi classify file --file /tmp/unknown.pdf --document-type receipt --document-type invoice
431+
```
432+
433+
You can also pipe binary file data via stdin by passing `--file -`:
434+
435+
```bash
436+
curl -s https://cdn.example.com/r.jpg | veryfi documents process --file -
437+
```
438+
439+
### Output and exit codes
440+
441+
Every command emits a JSON response on stdout. Use `--output raw` for single-line JSON (handy for piping into `jq`) or `--output pretty` for sorted keys. Errors are emitted as JSON on **stderr** and the process exits with a non-zero status:
442+
443+
| Exit code | Meaning |
444+
|-----------|---------|
445+
| `0` | Success |
446+
| `2` | Missing credentials or invalid CLI arguments |
447+
| `1`-`255` | Veryfi API error — exit code is the HTTP status (clipped to 255) |
448+
| `70` | Unexpected error (treat as a bug) |
449+
450+
The exact HTTP status is always included in the stderr payload, e.g.:
451+
452+
```json
453+
{
454+
"error": "Document not found",
455+
"status": 404,
456+
"exception": "ResourceNotFound"
457+
}
458+
```
459+
460+
### Passing arbitrary fields
461+
462+
For endpoints that accept `**kwargs` (e.g. `update_document`, `add_line_item`, `update_check`), use repeatable `--field KEY=VALUE` flags or `--json-body '<json>'`. `--field` values are JSON-decoded when possible (so `total=11.23` becomes a number, `enabled=true` becomes a boolean, `data='{"a":1}'` becomes an object) and fall back to plain strings.
463+
464+
### Discovery
465+
466+
Every command at every level supports `--help`, which lists subcommands or options with their descriptions:
467+
468+
```bash
469+
veryfi --help # top-level: lists all resource groups
470+
veryfi documents --help # group: lists process, list, get, tags, line-items, set, …
471+
veryfi documents process --help # leaf: lists every flag with its description
472+
```
473+
474+
For AI agents and tooling that prefer a machine-readable contract, `veryfi schema` emits a JSON manifest of every command, its description, and every parameter (name, type, required, repeatable). Agents can ingest this once to register Veryfi as a tool surface without parsing `--help` text:
475+
476+
```bash
477+
veryfi schema | jq '.commands[] | {name, help}'
478+
```
479+
480+
---
481+
369482
## Contributing
370483

371484
Contributions are welcome! To get started:
@@ -378,6 +491,8 @@ pip install -r requirements.txt
378491
pip install black pytest responses tox
379492
```
380493

494+
`requirements.txt` already includes `typer`, which is required for the `veryfi` CLI and its tests.
495+
381496
3. Make your changes, then run the test suite:
382497

383498
```bash

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
requests>=2.22.0
2+
typer>=0.12.0

setup.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ keywords = veryfi, veryfi.com, ocr api
1818

1919
[options]
2020
packages = find:
21+
install_requires =
22+
requests>=2.22.0
23+
typer>=0.12.0
2124

2225

2326
[entry_points]
2427
pbr.config.drivers =
2528
plain = pbr.cfg.driver:Plain
29+
console_scripts =
30+
veryfi = veryfi.cli:app

0 commit comments

Comments
 (0)