Metadata-Version: 2.5
Name: enrichednow
Version: 0.2.0
Summary: The official Python library for the EnrichedNow API
Project-URL: Homepage, https://enrichednow.com
Project-URL: Documentation, https://docs.enrichednow.com
Project-URL: Repository, https://github.com/enrichednow/enrichednow-python
Project-URL: Issues, https://github.com/enrichednow/enrichednow-python/issues
Author: EnrichedNow
License: MIT
License-File: LICENSE
Keywords: api,datasets,enrichednow,jobs
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Typing :: Typed
Requires-Python: >=3.9
Requires-Dist: httpx<1,>=0.23
Provides-Extra: dev
Requires-Dist: mypy>=1.8; extra == 'dev'
Requires-Dist: pytest>=7; extra == 'dev'
Description-Content-Type: text/markdown

# EnrichedNow Python library

[![CI](https://github.com/enrichednow/enrichednow-python/actions/workflows/ci.yml/badge.svg)](https://github.com/enrichednow/enrichednow-python/actions/workflows/ci.yml)

The official Python library for the [EnrichedNow API](https://docs.enrichednow.com): run scrapers, read their records,
and query the job-postings datasets.

- Sync and async clients with the same surface
- Typed models and typed errors; ships `py.typed`
- Automatic pagination, retries with backoff, and waiting for a run to end
- One runtime dependency: [`httpx`](https://www.python-httpx.org/)
- Python 3.9+

## Install

```bash
pip install enrichednow
```

## Quickstart

Create an API key in the console under **API keys** and put it in the environment:

```bash
export ENRICHEDNOW_API_KEY="en_live_..."
```

```python
from enrichednow import EnrichedNow

client = EnrichedNow()  # reads ENRICHEDNOW_API_KEY

result = client.scrape("ats.board", {"board_url": "https://boards.greenhouse.io/acme"})

print(result.run.status, result.run.record_count)
for record in result.records:
    print(record["title"])
```

`scrape` starts a run, waits for it to end and hands back the run with its records. Records are fetched page by
page as you iterate.

## The client

```python
from enrichednow import EnrichedNow

client = EnrichedNow(
    api_key="en_live_...",                    # default: the ENRICHEDNOW_API_KEY environment variable
    base_url="https://api.enrichednow.com",   # default
    timeout=30.0,                             # seconds for one HTTP request (or an httpx.Timeout)
    max_retries=2,                            # see "Retries"
)
```

Without a key, the constructor raises `MissingAPIKeyError`. Keep the key out of your code.

The client holds a connection pool. Use it as a context manager, or call `client.close()`, when you are done:

```python
with EnrichedNow() as client:
    print(client.health().status)  # "ok"
```

To bring your own `httpx.Client` (custom transport or TLS settings), pass `http_client=...`. The library does not
close a client it did not create.

## Scrapers

```python
for scraper in client.scrapers.list():          # the catalogue
    print(scraper.id, scraper.title, scraper.billing_unit)

scraper = client.scrapers.get("ats.board")      # with JSON Schemas
print(scraper.input_schema)                     # what goes in `input`
print(scraper.record_schema)                    # the shape of one record
```

### Start a run

```python
run = client.scrapers.run("careers.company", {"domain": "acme.com"})
print(run.id, run.status)                       # "queued"
```

`wait` (0 to 60 seconds) asks the API to hold the request open until the run ends or the time is up. Either way
you get the run as it is at that moment, so treat `queued` and `running` as ordinary answers:

```python
run = client.scrapers.run("careers.company", {"domain": "acme.com"}, wait=60)
if run.is_active:
    run = client.runs.wait(run.id)
```

The HTTP timeout of that one request is raised to `wait` + 30 seconds when yours is shorter.

## Runs

```python
run = client.runs.get("run_...")                # the run as it is now
run = client.runs.wait("run_...", timeout=600, poll_interval=2)   # poll until it has ended
run = client.runs.cancel("run_...")             # ask it to stop

for run in client.runs.list(scraper_id="ats.board", status="ok"):
    print(run.id, run.created_at, run.record_count)
```

A `Run` has `id`, `scraper_id`, `scraper_version`, `status`, `reason`, `input`, `record_count`, `units`,
`diagnostics`, `created_at`, `started_at` and `finished_at`, plus a few helpers:

| | |
| --- | --- |
| `run.is_active` | `queued` or `running` |
| `run.is_terminal` | the run has ended, however it ended |
| `run.succeeded` | ended `ok` or `partial` |
| `run.failed` | ended `failed`, `unsupported` or `cancelled` |
| `run.raise_for_status()` | raises `RunFailedError` when `run.failed`; otherwise returns the run |

`runs.wait` returns the run however it ended and raises `WaitTimeoutError` if it is still going after `timeout`
seconds. The run itself carries on; the error has it on `.run`.

Cancelling is cooperative: a run that was already finishing may still complete. Read the status it ended with.

### Records

```python
for record in client.runs.records(run.id):      # every record, page after page
    ...
```

Records are plain dicts in the shape of the scraper's `record_schema`. They are kept for 7 days after the run
ended; after that the list is empty and `page.expired` is `True`.

### CSV and NDJSON, written by the API

```python
with open("jobs.csv", "wb") as out:
    for chunk in client.runs.export_all(run.id, "csv"):  # every page; the header row once
        out.write(chunk)

page = client.runs.export(run.id, "ndjson", limit=500)  # one page: page.content, page.next_cursor, page.expired
```

The CSV has a column for each field of the scraper's record schema, in its order, so every page of every run of a
scraper has the same columns; a nested object becomes `parent.child` columns, a list one cell of JSON, and a cell
a spreadsheet would run as a formula starts with an apostrophe.

### Files

A scraper that makes a file (a screenshot) puts its address in the record. `client.runs.file(run.id, "screenshot.png")`
returns its bytes, read with your key.

### Waiting longer in one call

`wait` holds a request open until the run ends, up to 300 seconds, on starting a run and on reading one:
`client.scrapers.run(..., wait=120)`, `client.runs.get(run.id, wait=120)`. `runs.list` also takes `api_key_id`,
`from_` and `to`.

## The one-liner

```python
result = client.scrape(
    "ats.board",
    {"board_url": "https://boards.greenhouse.io/acme"},
    timeout=600,             # give up waiting after this many seconds (None: wait as long as it takes)
    poll_interval=2,
    raise_on_failure=True,
)
result.run        # the Run, ended
result.records    # iterable of records; `for record in result` works too
```

A run that ends `failed`, `unsupported` or `cancelled` raises `RunFailedError` with `.status`, `.reason` and
`.run`. Pass `raise_on_failure=False` to get the result instead. A run that ends `partial` is returned: it has
records, and `result.run.reason` says why it is not complete.

```python
from enrichednow import RunFailedError

try:
    result = client.scrape("careers.company", {"domain": "acme.com"})
except RunFailedError as error:
    print(error.status, error.reason, error.run.id)
```

## Webhooks

Instead of polling, have a URL of yours told when a run ends. An endpoint is an `https` URL and the events it
wants: `run.succeeded` (the status `ok`), `run.partial`, `run.failed`, `run.unsupported`, `run.cancelled`.

```python
endpoint = client.webhooks.create(
    "https://example.com/hooks/enrichednow",
    ["run.succeeded", "run.partial", "run.failed"],
    description="production",
)
print(endpoint.id, endpoint.secret)             # the signing secret (whsec_...) is shown only here: store it
```

```python
client.webhooks.list()                          # .endpoints (iterable) and .run_secret_set
client.webhooks.get("whk_...")
client.webhooks.update("whk_...", enabled=False)                # any of url, events, description, enabled
client.webhooks.rotate_secret("whk_...").secret                 # a new secret; the old one stops at once
client.webhooks.delete("whk_...")

for delivery in client.webhooks.deliveries.list(status="failed"):   # the delivery log, newest first
    print(delivery.id, delivery.event, delivery.attempts, delivery.error)
client.webhooks.deliveries.resend("whd_...")                    # send one again
```

### Verify a request

Anybody who learns your URL can post to it, so check the signature before you act on a request.
`verify_webhook` needs no client and makes no request. Give it the body exactly as it arrived, as raw bytes,
before any JSON parsing:

```python
import os

from flask import Flask, request

from enrichednow import WebhookVerificationError, parse_webhook

app = Flask(__name__)
SECRET = os.environ["ENRICHEDNOW_WEBHOOK_SECRET"]


@app.post("/hooks/enrichednow")
def run_ended():
    try:
        event = parse_webhook(
            SECRET,
            request.headers.get("X-Webhook-Timestamp", ""),
            request.get_data(),                                 # the raw bytes
            request.headers.get("X-Webhook-Signature", ""),
        )
    except WebhookVerificationError:
        return "", 400
    print(event.id, event.type, event.run.id, event.run.record_count)
    return "", 204
```

`parse_webhook` returns a `WebhookEvent` (`id`, `type`, `created_at`, `run`) and raises
`WebhookVerificationError` for a request that does not verify. `verify_webhook(secret, timestamp, body, signature)`
takes the same arguments and returns `True` or `False`. Both compare in constant time and refuse a timestamp more
than 5 minutes from now (`tolerance_s=300`).

An event can arrive more than once, and is sent again when you resend a delivery; `event.id` is the same every
time, so use it to ignore a repeat. Answer with a 2xx quickly and do the work afterwards.

### A webhook for one run

```python
client.webhooks.create_run_secret().secret      # once for the account; shown only here
run = client.scrapers.run("careers.company", {"domain": "acme.com"}, webhook_url="https://example.com/hooks/run-done")
```

`webhook_url` is told when that run ends, whichever way it ends, beside your endpoints. `client.scrape()` takes it
too. What is sent there is signed with the account's run secret; verify it the same way.

## Usage

```python
from datetime import date

usage = client.usage.get()                      # this calendar month (UTC), by day
print(usage.total_units, "of", usage.allowance_units)

usage = client.usage.get(from_=date(2026, 11, 1), to="2026-11-18", group_by="scraper")
for row in usage.rows:
    print(row.key, row.units, row.runs)

client.usage.get(product="data")                # the data allowance and its usage
```

`group_by` is `day`, `scraper` or `product`. The period's first day is `from_` here (`from` in the API, a reserved
word in Python); the same goes for `usage.period.from_`. `usage.unmetered` is `True` for an account with no unit
limit.

### Where the account stands

Every answer about runs carries the account's units and runs in progress in its headers. The client keeps the last
one it saw, and `usage.standing()` asks for it:

```python
standing = client.usage.standing()
print(standing.units_remaining, standing.units_topup, standing.units_reset, standing.runs_remaining)
print(standing.units_available)                 # what is left of the allowance, and the top-up units

client.scrapers.run("ats.board", {...})
client.standing                                 # as that answer said it, without another call
```

## Datasets

The datasets reads live under `client.data`. Each record returned costs one data unit, so stop iterating when you
have what you need.

```python
# Search postings
for posting in client.data.postings.list(title="software engineer", country="DE", status="open", limit=200):
    print(posting.title_clean, posting.company.name, posting.location.city, posting.url)

# One posting, with its description and history
posting = client.data.postings.get("...")
print(posting.description, posting.history.events)

# Companies
for company in client.data.companies.list(q="acme", ats="greenhouse"):
    print(company.domain, company.postings.open)

company = client.data.companies.get("acme.com")
for posting in client.data.companies.postings("acme.com", status="open"):
    ...
```

Posting filters: `company_domain`, `title`, `location`, `country`, `remote`, `employment_type`, `seniority`,
`function`, `status`, `source`, `provenance`, `unmatched`, `posted_from`, `posted_to`, `first_seen_from`,
`first_seen_to`, `last_seen_from`, `last_seen_to`, `sort`. Dates and times can be `date` / `datetime` objects or
ISO 8601 strings; `provenance` can be a string or a list (`["feed", "page"]`).

To follow what is new, ask for what changed since you last asked:

```python
new = client.data.postings.list(first_seen_from=last_checked)
seen = client.data.postings.list(sort="last_seen_at", last_seen_from=last_checked)
```

Two fields are renamed because the models keep the original JSON on `.raw`: the API's `location.raw` and
`salary.raw` are `location.raw_text` and `salary.raw_text`.

## Pagination

Every list returns a page. Iterating it yields its items and then those of every later page, fetching each page
as it is reached:

```python
for run in client.runs.list(limit=100):         # `limit` is the size of a page
    ...

everything = client.runs.records(run_id, limit=1000).to_list()
```

For one page at a time:

```python
page = client.runs.records(run_id, limit=500)
page.items                # this page's records
page.next_cursor          # None on the last page
page.has_next_page()
page = page.next_page()   # None on the last page

for page in client.data.postings.list(status="open").iter_pages():
    save(page.items)
```

To resume later, keep `page.next_cursor` and pass it back as `cursor=` with the same other arguments. A cursor is
an opaque string; pass it back unchanged.

## Errors

Two different things can go wrong, and they are reported differently:

- **The request failed**: the API answered 4xx or 5xx, or did not answer. The library raises.
- **The scrape failed**: the request was fine, and the run ended `failed` or `unsupported`. `runs.get` and
  `runs.wait` return that run; `scrape()` and `run.raise_for_status()` raise `RunFailedError`.

```text
EnrichedNowError
├── MissingAPIKeyError
├── APIConnectionError              the request never got an answer
│   └── APITimeoutError
├── APIError                        the API answered with an error
│   ├── AuthenticationError         401 unauthorized
│   │   └── PermissionDeniedError   403 forbidden
│   ├── NotFoundError               404 not_found
│   ├── RateLimitError              429 too_many_runs, rate_limited
│   ├── ValidationError             422 invalid_input, and any other 4xx
│   │   └── QuotaExceededError      402 quota_exceeded
│   └── ServerError                 5xx internal, data_unavailable
├── RunFailedError                  a run ended failed, unsupported or cancelled
├── WaitTimeoutError                a run did not end within the time given
└── WebhookVerificationError        a webhook request did not verify (parse_webhook)
```

An `APIError` carries what the API said:

```python
import enrichednow

try:
    client.scrapers.run("ats.board", {"board": "acme"})
except enrichednow.QuotaExceededError as error:
    print(error.used_units, error.allowance_units)
except enrichednow.ValidationError as error:
    print(error.status_code)    # 422
    print(error.code)           # "invalid_input"  <- match on this
    print(error.message)        # a sentence for a person
    print(error.fields)         # [{"path": "input.board_url", "message": "Field required"}, ...]
except enrichednow.RateLimitError as error:
    print(error.code)           # "too_many_runs" or "rate_limited"
    print(error.active_runs, error.max_concurrent_runs, error.retry_after)
except enrichednow.APIError as error:
    print(error.status_code, error.code, error.message, error.details, error.request_id)
```

| Attribute | |
| --- | --- |
| `status_code` | The HTTP status. |
| `code` | The API's stable error code. |
| `message` | A sentence for a person. Show it or log it; do not parse it. |
| `details` | Always a dict. `fields` for `invalid_input`; `used_units` and `allowance_units` for `quota_exceeded`; `active_runs` and `max_concurrent_runs` for `too_many_runs`. |
| `reason` | `details["reason"]` when the API gives one, such as `email_not_verified` on a 403. |
| `request_id` | The request id, when the answer has one; otherwise `None`. |
| `body`, `response` | The parsed body and the `httpx.Response`. |

## Retries

A request that fails in a way worth retrying is sent again, up to `max_retries` times (default 2), with
exponential backoff and jitter: about half a second, then a second, then two, up to eight.

| Failure | Retried |
| --- | --- |
| The connection failed or the request timed out | Yes |
| `429` | Yes, after the seconds in `Retry-After` when the answer has it |
| `5xx` | Yes |
| Any other `4xx` | No: the same request would get the same answer |

A `Retry-After` longer than 60 seconds is not waited for: the `RateLimitError` is raised, with the wait on
`.retry_after`.

**Starting a run is safe to repeat too.** `scrapers.run()` and `scrape()` send an `Idempotency-Key` with the
request, a new one for each call, so a retry after a timeout or a `5xx` gets the run the first attempt started and
never a second one. To make a call of your own repeatable (a job that may be restarted, say), pass your own key:
`client.scrape("ats.board", input, idempotency_key="import-2026-11-19-acme")`. For 24 hours the same key with the
same request gives the same run; the same key with a different request raises `ConflictError`.

`webhooks.create()` and `webhooks.deliveries.resend()` are the exception: they are sent again only when the API cannot have acted (the connection was never made, or the answer was `429`), because a repeat would add a second endpoint, or
queue the delivery twice.

```python
client = EnrichedNow(max_retries=0)   # never retry
client = EnrichedNow(max_retries=5)
```

## Async

`AsyncEnrichedNow` has the same resources, methods and arguments. Every call is awaited; lists are iterated with
`async for`.

```python
import asyncio
from enrichednow import AsyncEnrichedNow


async def main() -> None:
    async with AsyncEnrichedNow() as client:
        result = await client.scrape("ats.board", {"board_url": "https://boards.greenhouse.io/acme"})
        async for record in result.records:
            print(record["title"])

        run = await client.scrapers.run("careers.company", {"domain": "acme.com"}, wait=60)
        run = await client.runs.wait(run.id)

        async for posting in client.data.postings.list(status="open"):   # every item, page after page
            ...

        page = await client.runs.list(limit=50)                           # or one page
        page = await page.next_page()

        records = await client.runs.records(run.id).to_list()


asyncio.run(main())
```

## Models

Responses are frozen dataclasses with type hints. Timestamps are `datetime` objects (UTC), days are `date`
objects. Every model keeps the JSON it came from on `.raw` (also `.to_dict()`), so a field the API adds later is
there before this library knows its name, and never breaks parsing.

```python
run = client.runs.get("run_...")
run.finished_at                 # datetime
run.diagnostics.facts           # dict
run.raw                         # the JSON object, as returned
```

## Development

```bash
pip install -e ".[dev]"
pytest               # the tests
mypy                 # the strict type check
python -m build      # the wheel and the source distribution, into dist/ (pip install build)
```

The tests run against a mocked transport and make no network calls.

## Licence

MIT. See [LICENSE](LICENSE). To report a security problem, see [SECURITY.md](SECURITY.md).
