# Export data and download files (https://guides.dataportal.gov.lt/docs/api/guides/downloading)



Use the `export` route to download a table as a file. It accepts the same row and column options as a table [query](https://guides.dataportal.gov.lt/docs/api/guides/querying).

<MethodPath method="POST" path="/v1/data/organizations/{id}/tables/{table}/export?format=csv" />

```bash
curl -X POST 'https://api.dataportal.gov.lt/v1/data/organizations/188600177/tables/S3R167_M3010203_en/export?format=csv' \
  -H 'Content-Type: application/json' \
  -d '{}' \
  -o population.csv
```

<Callout title="Every matching row by default">
  An export has no row cap. With no body, `{}` or `"limit": null`, it returns every matching row: the example table's 137,640 rows come in one file. To export fewer, set `limit` to 1 or more (`0` returns `400`); to skip rows, set `offset`. A [query](https://guides.dataportal.gov.lt/docs/api/guides/querying) is different: it returns up to 100 rows by default and at most 100,000.
</Callout>

## Formats [#formats]

Set the file format with the `format` URL parameter.

<ParamTable
  columns="[&#x22;format=&#x22;, &#x22;File&#x22;, &#x22;When to use it&#x22;]"
  lang="en"
  params="[
  { name: &#x22;ndjson&#x22;, type: &#x22;default&#x22;, description: &#x22;One JSON object per line — easy to stream and process line by line.&#x22; },
  { name: &#x22;json&#x22;, type: &#x22;application/json&#x22;, description: &#x22;One JSON array of row objects.&#x22; },
  { name: &#x22;csv&#x22;, type: &#x22;text/csv&#x22;, description: &#x22;Comma-separated, with a header row.&#x22; },
  { name: &#x22;xlsx&#x22;, type: &#x22;Excel&#x22;, description: &#x22;An Excel workbook.&#x22; },
  { name: &#x22;parquet&#x22;, type: &#x22;Apache Parquet&#x22;, description: &#x22;Columnar and typed — the best choice for large tables in pandas, R, DuckDB or Spark.&#x22; },
]"
/>

An unsupported format returns `400` with the code `INVALID_OUTPUT_FORMAT`. Read `Content-Disposition` for the filename, for example `S3R167_M3010203_en.csv`.

## A filtered slice [#a-filtered-slice]

Use `select`, `filter` and `sort` to choose columns, filter rows and set their order.

```bash
curl -X POST 'https://api.dataportal.gov.lt/v1/data/organizations/188600177/tables/S3R167_M3010203_en/export?format=csv' \
  -H 'Content-Type: application/json' \
  -d "{\"select\":\"time_period,age_5_year_groups,sex,s3r167\",\"filter\":\"administrative_territory='Panevėžys c. mun.'\",\"sort\":\"-time_period\"}" \
  -o panevezys.zip
```

The file holds all 1,860 rows for Panevėžys city.

<Callout type="warn" title="When an export arrives zipped">
  When both conditions below apply, the API returns a ZIP archive. Check for `Content-Type: application/zip` and a filename ending in `.zip`.

  * the request **selects rows** — with `filter` or `exclude_nulls`;
  * the result has **128 rows or more**.

  Inside is the file as `S3R167_M3010203_en.part-00001.csv` (or `.json`, `.ndjson`,
  `.xlsx`), with its header row. Everything else comes as the plain file: exports
  without a row filter (`select` and `sort` alone do not count), filtered results
  under 128 rows, and **Parquet**, which is never zipped. When in doubt, check the
  `Content-Type` before you open the result.
</Callout>

The Python example below uses `requests` and `pandas`.

```python
import io, zipfile
import pandas as pd
import requests

URL = "https://api.dataportal.gov.lt/v1/data/organizations/188600177/tables/S3R167_M3010203_en/export"
body = {"filter": "administrative_territory='Panevėžys c. mun.'"}

resp = requests.post(URL, params={"format": "csv"}, json=body)
resp.raise_for_status()
if resp.headers["Content-Type"].startswith("application/zip"):
    with zipfile.ZipFile(io.BytesIO(resp.content)) as z:
        df = pd.concat(pd.read_csv(z.open(name)) for name in z.namelist())
else:
    df = pd.read_csv(io.BytesIO(resp.content))
print(len(df))  # 1860
```

## Query or export? [#query-or-export]

<ParamTable
  columns="[&#x22;&#x22;, &#x22;Returns&#x22;, &#x22;When to use it&#x22;]"
  names="text"
  lang="en"
  params="[
  { name: &#x22;Query&#x22;, type: &#x22;JSON in pages&#x22;, description: &#x22;For apps and scripts that read rows as they go, or need total before fetching.&#x22; },
  { name: &#x22;Export&#x22;, type: &#x22;a file&#x22;, description: &#x22;Every matching row in one file, in the format your tool reads best.&#x22; },
]"
/>

## Stored files [#stored-files]

An organisation may also store documents, archives or images. List its files first, then download one using its exact name.

<MethodPath method="GET" path="/v1/data/organizations/{id}/files" />

<MethodPath method="GET" path="/v1/data/organizations/{id}/files/{file}" />

```bash
curl 'https://api.dataportal.gov.lt/v1/data/organizations/188600177/files'
# { "data": { "organization": "188600177", "files": [] } }
```

An empty list means the organisation has no stored files. Requesting a filename that does not exist returns `404`.

## See also [#see-also]

* [Querying tables](https://guides.dataportal.gov.lt/docs/api/guides/querying)
* [Recipes](https://guides.dataportal.gov.lt/docs/api/guides/recipes)
* [Export data table rows](https://guides.dataportal.gov.lt/docs/api/data/export-table) (reference)
