Metadata-Version: 2.5
Name: companystack
Version: 0.1.0
Summary: Python client for the CompanyStack API: UK company data from Companies House (profiles, officers, PSCs, filings, financials, health scores, KYB, change feed, lists).
Project-URL: Homepage, https://github.com/CompanyStackLtd/companystack-python
Project-URL: Repository, https://github.com/CompanyStackLtd/companystack-python
Project-URL: Issues, https://github.com/CompanyStackLtd/companystack-python/issues
Project-URL: Documentation, https://companystack.co.uk/docs
Project-URL: API reference, https://api.companystack.co.uk/docs
Project-URL: Free API key, https://companystack.co.uk/signup?product=api&utm_source=pypi
Project-URL: Pricing, https://companystack.co.uk/pricing?product=api&utm_source=pypi
Author-email: CompanyStack Limited <support@companystack.co.uk>
License-Expression: MIT
License-File: LICENSE
Keywords: api-client,beneficial-ownership,companies-house,company-data,companystack,due-diligence,kyb,psc,uk
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
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: Topic :: Office/Business :: Financial
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: httpx<1,>=0.25
Requires-Dist: typing-extensions>=4.7
Provides-Extra: test
Requires-Dist: mypy>=1.10; extra == 'test'
Requires-Dist: pytest>=8; extra == 'test'
Description-Content-Type: text/markdown

# CompanyStack for Python

The Python client for the [CompanyStack API](https://companystack.co.uk/docs?utm_source=pypi): UK company data from Companies House. It covers company search and profiles, officers, persons with significant control, filing history, financials parsed from filed accounts, health scores, KYB checks, the register change feed and your company lists.

- Typed: every response model is a `TypedDict`, so mypy and your editor know the fields.
- Sync (`CompanyStack`) and asyncio (`AsyncCompanyStack`) clients on [httpx](https://www.python-httpx.org/).
- Retries a 429 after `Retry-After`, and retries 5xx and network errors on reads.
- Pagination helpers (`iter_*`) for search, filings, list items, the change feed, KYB checks and prospects.

Requires Python 3.9 or later.

## Install

```bash
pip install companystack
```

## Quick start

Get an API key (it starts with `cs_`) by [signing up free](https://companystack.co.uk/signup?product=api&utm_source=pypi): no card, 1,000 requests a day on the Free plan. Then set it in `COMPANYSTACK_API_KEY`, or pass `api_key=`.

```python
from companystack import CompanyStack

cs = CompanyStack()  # reads COMPANYSTACK_API_KEY

results = cs.search_companies(q="tesco", limit=5)
for company in results["results"]:
    print(company["company_number"], company["company_name"], company["company_status"])

profile = cs.get_company("00445790")
print(profile["accounts"]["next_due"])
```

## Examples

```python
# Officers and beneficial owners (Starter plan and above)
officers = cs.list_officers("00445790")
pscs = cs.list_persons_with_significant_control("00445790")

# Financials from filed accounts. Money values are whole pounds (GBP) as filed, not pence.
history = cs.get_financial_history("06450847")
for period in history["periods"]:
    print(period["balance_sheet_date"], period["net_assets"], period["turnover"])

# Health score: a rules-based 0-100 indicator (not a credit rating)
score = cs.get_health_score("06450847")
print(score["overall_score"], score["grade"], score["risk_level"])

# KYB check. Each call uses quota and stores a record on your account.
check = cs.run_kyb_check({"company_number": "06450847", "expected_name": "Club Free Spirit Limited", "reference": "client-42"})
print(check["outcome"], check["reason_codes"])

# Every search result, fetching pages as you go
for company in cs.iter_search_companies(q="bakery", town="LEEDS", max_items=200):
    print(company["company_name"])

# The change feed: store the cursor and resume from it next time
page = cs.list_changes(types="added,dissolved", postcode="LS")
cursor = page["next_cursor"]

# Your lists
lists = cs.list_lists()
cs.add_list_items(42, {"items": [{"company_number": "00445790", "custom_ref": "CL-0042"}]})
```

### asyncio

```python
import asyncio
from companystack import AsyncCompanyStack

async def main():
    async with AsyncCompanyStack() as cs:
        company = await cs.get_company("00445790")
        async for event in cs.iter_list_changes(types="added", max_items=100):
            print(event["company_number"], event["change_type"])

asyncio.run(main())
```

## Errors

Every failure raises a subclass of `CompanyStackError`, which carries `status`, `code`, `body`, `upgrade_url` and `retry_after`:

| Exception | When |
| --- | --- |
| `AuthenticationError` | 401: the key is missing, malformed or revoked |
| `PermissionDeniedError` | 403: your plan does not include the endpoint (`code == "plan_upgrade_required"`; see `upgrade_url`) |
| `NotFoundError` | 404 |
| `ValidationError` | 400 or 422 |
| `ConflictError` | 409 |
| `RateLimitError` | 429 after the retries; `retry_after` says how long to wait |
| `ServerError` | 5xx |
| `APIConnectionError` | No response (network error or timeout) |

```python
from companystack import CompanyStack, PermissionDeniedError, RateLimitError

try:
    cs.get_health_score("00445790")
except PermissionDeniedError as e:
    print("Upgrade at", e.upgrade_url)
except RateLimitError as e:
    print("Try again in", e.retry_after, "seconds")
```

## Configuration

```python
CompanyStack(
    api_key="cs_…",          # default: COMPANYSTACK_API_KEY
    base_url="https://api.companystack.co.uk",
    timeout=30.0,            # seconds per attempt
    max_retries=2,           # 429 on any method; 5xx and network errors on GET
    max_retry_delay=60.0,    # cap on Retry-After waits
    http_client=None,        # bring your own httpx.Client (proxies, transports)
)
```

After each call, `cs.last_rate_limit` holds the rate-limit headers (`limit`, `remaining`, `reset`, `daily_limit`, `daily_remaining`). What each plan includes, and its limits, is on the [pricing page](https://companystack.co.uk/pricing?product=api&utm_source=pypi).

## Development

`_generated.py` is generated from the [OpenAPI specification](https://api.companystack.co.uk/openapi.json) and is replaced on each release, so send changes to `_base.py` (the runtime) or open an issue.

```bash
pip install -e ".[test]"
pytest            # runs against a local mock server; no network, no key
mypy
```

## Pricing

Free API plan: £0, 1,000 requests a day, 30 a minute, no card: search (first page, up to 10 results), autocomplete and full company profiles. Starter: £29/month or £288/year, adding officers, owners (PSC), filing history, filed accounts and health scores. Pay as you go, without a monthly plan: prepaid credits from £10 (1,000 credits), used only for calls your plan does not include. Current prices and limits: [companystack.co.uk/pricing](https://companystack.co.uk/pricing?product=api).

## About the data

The data is a copy of public registers (Companies House and the others listed at [companystack.co.uk/data-sources](https://companystack.co.uk/data-sources)), as CompanyStack holds them, and can be incomplete or out of date. Register details and accounts figures are the company's own filings, shown as filed: CompanyStack does not audit or verify them. Health scores and KYB results are indicators that support your own checks, not credit ratings or advice.

Contains public sector information licensed under the [Open Government Licence v3.0](https://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/). CompanyStack is not affiliated with or endorsed by Companies House.

## Licence

MIT
