2invoice Python API Docs | dltHub

Build a 2invoice-to-database pipeline in Python using dlt with AI Workbench support for Claude Code, Cursor, and Codex.

Last updated:

2invoice is a Romanian invoicing/platform API for creating and managing invoices, clients, products, receipts and related accounting documents. The REST API base URL is https://api.2invoice.ro and all requests require a Bearer token for authentication.

dlt is an open-source Python library that handles authentication, pagination, and schema evolution automatically. dlthub provides AI context files that enable code assistants to generate production-ready pipelines. Install with uv add "dlt[hub]" and start loading 2invoice data in under 10 minutes.


What data can I load from 2invoice?

Here are some of the endpoints you can load from 2invoice:

ResourceEndpointMethodData selectorDescription
invoicesinvoicesGETinvoicesList invoices (paginated)
invoiceinvoices/{invoice}GETGet single invoice details
clientsclientsGETclientsList clients
clientclients/{client}GETGet single client details
productsproductsGETdataList products (returns {"data": [...]})
receiptsreceiptsGETreceiptsList receipts (paginated; receipts.data contains records)
document_seriesdocument-seriesGETdocument_seriesList document series
branch_officesbranch-officesGETbranch_officesList company branch offices
inventoriesinventoriesGETinventoriesList inventories
templatestemplatesGETtemplatesList templates

How do I authenticate with the 2invoice API?

Create an API user in the 2invoice account, generate an access token via POST auth/generate-token, then include the token in the Authorization header as: Authorization: Bearer <access_token>. Tokens expire (example: 1 year).

1. Get your credentials

  1. Log in to your 2invoice account dashboard. 2) Create an API user/credentials in the account settings/API section. 3) Call POST https://api.2invoice.ro/auth/generate-token (Accept: application/json) to generate an access_token. 4) Store the returned access_token and add it to requests as Authorization: Bearer <access_token>. 5) Optionally call GET auth/token-information to inspect token, and use revoke endpoints if needed.

2. Add them to .dlt/secrets.toml

[sources.api_2invoice_source] access_token = "your_access_token_here"

dlt reads this automatically at runtime — never hardcode tokens in your pipeline script. For production environments, see setting up credentials with dlt for environment variable and vault-based options.


How do I set up and run the pipeline?

Set up a virtual environment and install dlt:

uv init uv add "dlt[hub]"

1. Install the dlt AI Workbench:

uv run dlthub ai init --agent <your-agent> # <agent>: claude | cursor | codex

This installs project rules, a secrets management skill, appropriate ignore files, and configures the dlt MCP server for your agent. Learn more →

2. Install the rest-api-pipeline toolkit:

uv run dlthub ai toolkit install rest-api-pipeline

This loads the skills and context about dlt the agent uses to build the pipeline iteratively, efficiently, and safely. The agent uses MCP tools to inspect credentials — it never needs to read your secrets.toml directly. Learn more →

3. Start LLM-assisted coding:

Use /find-source to load data from the 2invoice API into DuckDB.

The rest-api-pipeline toolkit takes over from here — it reads relevant API documentation, presents you with options for which endpoints to load, and follows a structured workflow to scaffold, debug, and validate the pipeline step by step.

4. Run the pipeline:

uv run python api_2invoice_pipeline.py

If everything is configured correctly, you'll see output like this:

Pipeline api_2invoice_pipeline load step completed in 0.26 seconds 1 load package(s) were loaded to destination duckdb and into dataset api_2invoice_data The duckdb destination used duckdb:/api_2invoice.duckdb location to store data Load package 1749667187.541553 is LOADED and contains no failed jobs

Inspect your pipeline and data:

uv run dlthub show

This opens the Pipeline Dashboard where you can verify pipeline state, load metrics, schema (tables, columns, types), and query the loaded data directly.


Python pipeline example

This example loads invoices and clients from the 2invoice API into DuckDB. It mirrors the endpoint and data selector configuration from the table above:

import dlt from dlt.sources.rest_api import RESTAPIConfig, rest_api_resources @dlt.source def api_2invoice_source(access_token=dlt.secrets.value): config: RESTAPIConfig = { "client": { "base_url": "https://api.2invoice.ro", "auth": { "type": "bearer", "access_token": access_token, }, }, "resources": [ {"name": "invoices", "endpoint": {"path": "invoices", "data_selector": "invoices"}}, {"name": "clients", "endpoint": {"path": "clients", "data_selector": "clients"}} ], } yield from rest_api_resources(config) def get_data() -> None: pipeline = dlt.pipeline( pipeline_name="api_2invoice_pipeline", destination="duckdb", dataset_name="api_2invoice_data", ) load_info = pipeline.run(api_2invoice_source()) print(load_info)

To add more endpoints, append entries from the resource table to the "resources" list using the same name, path, and data_selector pattern.


How do I query the loaded data?

Once the pipeline runs, dlt creates one table per resource. You can query with Python or SQL.

Python (pandas DataFrame):

import dlt data = dlt.pipeline("api_2invoice_pipeline").dataset() sessions_df = data.invoices.df() print(sessions_df.head())

SQL (DuckDB example):

SELECT * FROM api_2invoice_data.invoices LIMIT 10;

In a marimo or Jupyter notebook:

import dlt data = dlt.pipeline("api_2invoice_pipeline").dataset() data.invoices.df().head()

See how to explore your data in marimo Notebooks and how to query your data in Python with dataset.


What destinations can I load 2invoice data to?

dlt supports loading into any of these destinations — only the destination parameter changes:

DestinationExample value
DuckDB (local, default)"duckdb"
PostgreSQL"postgres"
BigQuery"bigquery"
Snowflake"snowflake"
Redshift"redshift"
Databricks"databricks"
Filesystem (S3, GCS, Azure)"filesystem"

Change the destination in dlt.pipeline(destination="snowflake") and add credentials in .dlt/secrets.toml. See the full destinations list.


Troubleshooting

Authentication failures

If you receive 401 Unauthorized: ensure you generated an access token via POST /auth/generate-token and supply the header Authorization: Bearer <access_token>. Tokens expire (examples show 1 year) — regenerate if expired.

Rate limits

2invoice returns 429 Too Many Requests when you exceed request rates. Implement exponential backoff and retry logic.

Pagination and data selectors

Many list endpoints are paginated. Some endpoints return a top-level plural key (e.g. receipts -> receipts, templates -> templates, document-series -> document_series, inventories -> inventories). Paginated responses often wrap records under a pagination object (e.g. receipts -> receipts.data). For products the list response uses key data ("data": [...]). When building selectors, inspect the top-level response for the plural key or the data subkey (e.g. response['invoices'] or response['invoices']['data']).

Common HTTP errors

400 Bad Request — missing/invalid parameters. 401 Unauthorized — invalid/expired token. 404 Not Found — resource id does not exist. 409 Conflict — conflicting request. 500/502/503/504 — server errors; retry with backoff.

Ensure that the API key is valid to avoid 401 Unauthorized errors. Also, verify endpoint paths and parameters to avoid 404 Not Found errors.


Next steps

Continue your data engineering journey with the other toolkits of the dltHub AI Workbench:

  • data-exploration — Build custom notebooks, charts, and dashboards for deeper analysis with marimo notebooks.
  • dlthub-runtime — Deploy, schedule, and monitor your pipeline in production.
uv run dlthub ai toolkit install data-exploration uv run dlthub ai toolkit install dlthub-runtime

Was this page helpful?

Community Hub

Need more dlt context for 2invoice?

Request dlt skills, commands, AGENT.md files, and AI-native context.