Metadata-Version: 2.5
Name: kobo-api-v2
Version: 0.2.0
Summary: A synchronous Python client for the KoboToolbox API v2
Project-URL: Homepage, https://github.com/morabdiego/kobo-api-v2
Project-URL: Repository, https://github.com/morabdiego/kobo-api-v2
Project-URL: Bug Tracker, https://github.com/morabdiego/kobo-api-v2/issues
Author-email: Diego Mora <morabdiego@gmail.com>
License: MIT
License-File: LICENSE
Keywords: api,data-collection,kobo,kobotoolbox,survey,xlsform
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
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 :: Internet :: WWW/HTTP
Classifier: Topic :: Scientific/Engineering
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: requests>=2.32
Description-Content-Type: text/markdown

# Kobo API v2 Python client

[![PyPI version](https://img.shields.io/pypi/v/kobo-api-v2.svg)](https://pypi.org/project/kobo-api-v2/)
[![Python](https://img.shields.io/pypi/pyversions/kobo-api-v2.svg)](https://pypi.org/project/kobo-api-v2/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

A minimal, synchronous Python client for downloading surveys from the [KoboToolbox API v2](https://eu.kobotoolbox.org/api/v2/docs/).

## Installation

```bash
pip install kobo-api-v2
```

Requires Python ≥ 3.10.

## Quick start

```python
from kobo import KoboClient

client = KoboClient(api_token="<your_api_token>")

# List available surveys
surveys = client.get_surveys()

# Download the first survey as Excel in one synchronous call
saved = client.get_excel(surveys[0]["uid"])
print(saved)  # → /absolute/path/to/data_survey.xlsx
```

Your API token is under **Account Settings → Security → API key** in your KoboToolbox account.

---

## API reference

### `KoboClient(api_token, base_url=...)`

```python
# Global server (default)
client = KoboClient(api_token="...")

# European Union server
client = KoboClient(api_token="...", base_url="https://eu.kobotoolbox.org")

# Self-hosted instance
client = KoboClient(api_token="...", base_url="https://kobo.myserver.org")
```

---

### `get_surveys(limit=100, offset=0, search=None)`

```python
surveys = client.get_surveys()
surveys = client.get_surveys(search="census", limit=50, offset=0)
```

| Parameter | Default | Description |
|-----------|---------|-------------|
| `limit` | `100` | Results per page (API max: 300) |
| `offset` | `0` | Pagination offset |
| `search` | `None` | Free-text filter |

---

### `get_excel(asset_uid, path="data_survey.xlsx", setting_name="python-client", **overrides)`

Downloads survey data synchronously as an `.xlsx` file using a reusable KoboToolbox export setting (no async tasks or polling):

```python
# Minimal — saves to data_survey.xlsx in the current directory
saved = client.get_excel(uid)

# Custom output path and pipeline-friendly export settings
saved = client.get_excel(
    uid,
    path="outputs/survey.xlsx",
    setting_name="pipeline_v1",
    group_sep="_",
    multiple_select="details",
    xls_types_as_text=True,
)
```

| Parameter | Default | Description |
|-----------|---------|-------------|
| `asset_uid` | — | Survey UID |
| `path` | `"data_survey.xlsx"` | Local destination path |
| `setting_name` | `"python-client"` | Name of the reusable export setting in KoboToolbox |
| `**overrides` | — | Export setting overrides applied when creating a new named setting |

Default export settings (overridable via `**overrides`):

| Field | Default | Description |
|-------|---------|-------------|
| `lang` | `"_xml"` | `"_xml"` = XLSForm internal names; or a language label, e.g. `"English"` |
| `fields_from_all_versions` | `True` | Include fields from older form versions |
| `group_sep` | `"/"` | Column name separator for groups |
| `hierarchy_in_labels` | `False` | Prepend group hierarchy to labels |
| `multiple_select` | `"summary"` | `"summary"`, `"both"`, or `"details"` |
| `flatten` | `False` | Flatten group structure |
| `xls_types_as_text` | `False` | Export all values as strings |
| `include_media_url` | `False` | Include attachment download URLs |
| `fields` | `[]` | Specific fields to export (empty = all) |
| `query` | `{}` | MongoDB-style server-side filter |

---

## Error handling

```python
from kobo import KoboClient, KoboError

try:
    saved = client.get_excel(uid, path="out.xlsx")
except KoboError as e:
    print(f"API error: {e}")
```

---

## Documentation

Detailed guides (in Spanish) are available in [`docs/`](docs/README.md):

- [Overview & client architecture](docs/README.md)
- [Exports & pipeline configuration](docs/exports.md) — deep dive into every export setting and best practices for data pipelines
- [Examples notebook (`ejemplos.ipynb`)](docs/ejemplos.ipynb) — runnable walkthrough

---

## License

MIT — see [LICENSE](LICENSE).
