# Serve data from exported files

dcupl works entirely on files. An app or a REST API instance loads CSV and
JSON files from your project and answers every query from them; it never
calls the system the data came from. That makes dcupl a way to put data from
an ERP, a PIM or a legacy database in front of users and agents while that
system stays where it is: inside your network, with no route in from the
internet.

## Why files help

A source system that was never built to face the internet is a risk once it
does: an old web stack, an unpatched API, a database with broad accounts.
With exported files, nothing outside your network talks to it:

- **No inbound access.** The source system, or a job next to it, exports
  snapshots and sends them out over HTTPS. No firewall rule lets anything
  in, and dcupl holds no credentials for the source system.
- **One way.** Files go from your network to your dcupl project, never back.
  A query, a scripted endpoint or an agent reads the files; none of them can
  reach the source system, change its data or load it.
- **Only what you export.** The export decides which tables, columns and rows
  leave your network. Purchase prices or personal data that are not in the
  file are not in dcupl.

```mermaid
flowchart LR
  subgraph Internal["Your network: no inbound access"]
    S["Source system<br/>ERP · PIM · database"]
    E["Export job<br/>CSV / JSON snapshots"]
  end
  subgraph Project["Your dcupl project"]
    F["Project files<br/>data/*.csv · models · dcupl.lc.json"]
  end
  subgraph Readers["Readers"]
    R["REST API instance"]
    B["Browser app"]
  end
  S --> E
  E -->|"outbound HTTPS<br/>push or write"| F
  F --> R
  F --> B
```

## The pattern

1. The source system exports snapshots on a schedule.
2. A job pushes the files to your project, or a workflow picks them up,
   transforms them and writes them.
3. The loader configuration points at the files.
4. A REST API instance or an app loads them.

### 1. Export snapshots

Export one file per model, as CSV or JSON in UTF-8. The dcupl CDN stores text
files only (see [Cloud sync & configuration](/cli/cloud-sync)).
Give every row a stable key, such as the article number, and keep the file
names the same from one export to the next, so the loader configuration never
changes.

Export full snapshots rather than changes: each export replaces the file, so
a row the source system deleted is gone from dcupl after the next load.

### 2. Get the files into your project

Pick the way that fits what may run next to the source system.

#### CLI push

Install the CLI on the export host (`npm install -g @dcupl/cli@2`) and run
`dcupl files push` after each export. The host needs outbound HTTPS only.

```bash filename="export-and-push.sh"
#!/bin/sh
set -e
# DCUPL_PROJECT_ID and DCUPL_API_KEY come from the host's secret store
cd /opt/dcupl-export        # the workspace root; the export writes into dcupl/data/
./run-export.sh             # your export
dcupl files push --version draft --path data --force --yes
```

- `push` needs the project's **CLI** key (type CLI read/write, see
  [API keys](/console/api-keys)). That key reads and writes every file of the
  project, so keep it in the host's secret store, never in the script.
- `--force` lets the export win: it overwrites the server copy even when
  someone edited the file in the Console since the last push.
- `--path data` limits the push to the export folder. Paths are relative to
  the workspace's `baseFolder`, `dcupl/` by default, also in a folder
  without a `dcupl.config.json`.
- Push never deletes a server file. When the export drops a file for good,
  remove it with `dcupl files delete --path <path> --yes`.

See [CI/CD integration](/cli/ci-cd) for the same job in a pipeline.

#### CLI write, narrower key

When the export host should hold a key that can do less than the CLI key,
create a **scoped** key with `data/**` as its only file write pattern and
as a file read pattern too (the check step below lists the written files
through it), and write each file with `dcupl files write`:

```bash filename="export-and-write.sh"
#!/bin/sh
set -e
# DCUPL_PROJECT_ID and DCUPL_API_KEY (the scoped key) come from the host's secret store
./run-export.sh
dcupl files write --version draft --path data/articles.csv --file exports/articles.csv
dcupl files write --version draft --path data/prices.csv --file exports/prices.csv
```

The scoped key can write only the paths its write patterns cover, and it
can't push, pull or copy versions: `files status`, `push` and `pull` reject
every key type but CLI. See
[A scoped key carries globs over your files](/console/api-keys#a-scoped-key-carries-globs-over-your-files).

#### Workflow

When the export needs cleaning or reshaping, or the source system can't run
the CLI, let a [workflow](/console/workflows) do it. The source system still
only sends data out:

- it drops the export into an S3 bucket or an Azure Blob Storage container it
  already writes to, and a **Scheduled** trigger runs the workflow, which
  reads the file with an **S3 Files** or **Azure Files** node; or
- it posts the export to the workflow's **HTTP trigger**.

A **Script** node transforms the rows, and a **dcupl Files** node writes the
result to the project, with `version` set to `draft` and a key that writes
`data/**`, such as the project's Workflow Execution Key (see
[Node types](/console/workflows/reference#node-types)).

A run works within the [workflow limits](/console/workflows/reference#limits),
by default 10 s and 128 MB, so this fits exports of moderate size. Push
larger exports with the CLI.

### 3. Point the loader at the files

Add one resource per file to `dcupl.lc.json`. Its path stays the same from one
export to the next, and the models describe the columns:

```json filename="dcupl.lc.json"
{
  "resources": [
    { "url": "${baseUrl}/models/article.dcupl.json", "type": "model" },
    {
      "url": "${baseUrl}/data/articles.csv",
      "type": "data",
      "model": "Article",
      "options": { "keyProperty": "articleNumber", "csvParserOptions": { "delimiter": ";" } }
    }
  ]
}
```

The CSV parser detects `,`, `;`, tab or `|` from the first line; set
`csvParserOptions.delimiter` when your export's first line could mislead it.
See [Loader configuration](/console/loader) for every option.

### 4. Serve the files

- **A [REST API](/console/rapi) instance** loads the project's `draft`
  version, once, when it starts. After each export, select the instance on
  the runner's tab and click **Update**; it answers from the previous files
  until the reload is done.
- **A browser app** loads the files on every page load. It reads `draft`
  unless you set `version` (see
  [Load from the console](/sdk/guides/load-from-console)). For an app on
  `production`, promote the export after the push with
  `dcupl files versions copy --from draft --to production`, which needs the
  CLI key and replaces the contents of `production`.

## Trade-offs

- **Data is as fresh as the export.** An export every hour means data up to
  an hour old, plus the time until the instance reloads. Users who must see a
  change at once need a live source, not a file.
- **Read-only.** dcupl serves the snapshot. Changes go into the source system
  and reach dcupl with the next export; nothing flows back.
- **A multi-file export doesn't land at once.** The files arrive one after
  another, so a reader that loads in between sees new articles with old
  prices. Reload the instance after the last file, and on a schedule, make
  the export finish before the reload.
- **Sensitive files need a private project and scoped keys.** On a project
  with Private CDN (the default for new projects), the CDN serves files only
  to requests with one of the project's keys. A browser app's key is public,
  so files with purchase prices or customer data belong outside its read
  patterns (see [Two audiences in one project](/console/api-keys#two-audiences-in-one-project)).
- **Per-customer data needs your backend.** An export with every customer's
  rows doesn't become per-customer by itself: see
  [Per-customer data](/learn/best-practices/per-customer-data).
- **Size is bounded.** `push` uploads files of up to 500 MB each, and a REST
  API instance holds its data in memory: plan with
  [Sizing and limits](/console/rapi#sizing-and-limits).

## Check the setup

- The source system's firewall has no inbound rule for the export: the job
  connects out, nothing connects in.
- After a push or write, `dcupl files status --version draft --path data` on
  the export host lists no uploads. With a scoped key, which `files status`
  rejects, check with `dcupl files list --version draft` instead: it lists
  only the files the key's read patterns cover, so the key needs `data/**`
  among them to see the written files.
- The instance's [status endpoint](/console/rapi#status-endpoint) shows
  `ready`, and its `report` names no skipped resource.

## Next steps

- [CI/CD integration](/cli/ci-cd) — the push job, with credentials from a secret store
- [Cloud sync & configuration](/cli/cloud-sync) — every flag of `files push` and the version rules
- [API keys](/console/api-keys) — which key reads and writes which files
- [REST APIs](/console/rapi) — deploy and update the instance that serves the files
- [Per-customer data](/learn/best-practices/per-customer-data) — when each customer may see only its own rows
