ETLMCPData Conveyance Works
Log in Start free
Docs

Recipe: backfill several years from a script, one year at a time

How to script a multi-year backfill over the REST API: split it into ranges of at most a year, run them one after another, and re-run any partial range.

To load several years of history into a pipeline from a script, cut the period into ranges of at most 366 days, start one range with Start a run, poll it with Get a run until it ends, and only then start the next. etlmcp refuses a longer range, and it refuses a second run of the same pipeline while one is queued or running, so your script has to wait on each run.

The rules your loop has to respect

Runs started from the dashboard, the REST API and MCP pass the same checks. Four of them decide how you split the work.

  • Range length. A range counts both its first and its last day. One longer than 366 days returns 400 with a sync can cover at most 366 days. Calendar years fit, leap years included.
  • One run per pipeline. The database allows a single queued or running run for each pipeline, and the daily scheduled sync uses that same slot. Starting another returns 409 with a sync is already running for this pipeline.
  • The plan's history window. The first day may not be older than the days your plan keeps, counted back from today's date in UTC, or from yesterday while the pipeline's timezone is still on the previous day. Otherwise the answer is 400 with, for example, the Scale plan keeps 3650 days of history. Free keeps 30 days, Starter 365, Team 365, Agency 730 and Scale 3650. See the plans.
  • No future days. An end after today returns 400 with end must not be in the future.

Two more can stop a loop. Runs started by hand count against your organization's cap on queued or running manual runs (10 on Free); over it, the answer is 409 with 10 syncs are already queued or running: wait for one. And each credential may make 120 requests a minute, REST and MCP counted together; past that you get 429 with rate limit exceeded; retry later and a Retry-After header that says how many seconds to wait.

Every error body is JSON with a single error field that holds the message.

Plan the ranges

Start from the oldest day your plan allows and walk forward. The history window moves forward a day every day, so a range at the very edge that you leave until last can drift outside the window before your script reaches it.

Some platforms serve no data before a certain date. If a range falls wholly before it, the start is refused with {…} only provides data from {…}, naming the platform and the date. If a range only starts before it, the run quietly moves its first day up to that date. Either way, nothing before that date is pulled or replaced.

The loop

This sketch uses curl and jq against https://app.etlmcp.com/api/v1, with your key in $ETLMCP_API_KEY and a key that has the run scope. It runs one range per calendar year, oldest first, and assumes the Scale plan, whose window reaches back past 2024.

API=https://app.etlmcp.com/api/v1
PIPE=your-pipeline-id
AUTH="Authorization: Bearer $ETLMCP_API_KEY"

run_range() {   # $1 = start, $2 = end, both YYYY-MM-DD
  while true; do
    resp=$(curl -s -w '\n%{http_code}' -X POST "$API/pipelines/$PIPE/runs" \
      -H "$AUTH" -H 'Content-Type: application/json' \
      -d "{\"start\":\"$1\",\"end\":\"$2\"}")
    code=$(tail -n1 <<<"$resp"); body=$(sed '$d' <<<"$resp")
    case $code in
      202) run=$(jq -r .id <<<"$body"); break ;;
      409|429) sleep 60 ;;                      # another run holds the slot, or slow down
      *) echo "refused $1..$2: $(jq -r .error <<<"$body")"; return 1 ;;
    esac
  done
  while true; do
    status=$(curl -s -H "$AUTH" "$API/runs/$run" | jq -r .status)
    case $status in
      queued|running) sleep 30 ;;
      *) echo "$1..$2 $status"; [ "$status" = success ]; return ;;
    esac
  done
}

run_range 2024-01-01 2024-12-31 || run_range 2024-01-01 2024-12-31
run_range 2025-01-01 2025-12-31

Polling every half minute keeps one script far below the per-credential limit. A 409 can be the daily scheduled sync holding the pipeline's slot, or your organization's manual-run cap; both clear on their own, so the sketch waits and tries again. A 409 with this pipeline is paused by the plan's limits does not clear by waiting, so in a real script, read the error text before you retry.

What each final status means

A run ends as success, partial or failed. The other fields of the run object are on the Get a run page.

  • success: the range is stored and delivered. Move on.
  • partial: the rows were pulled and stored, but delivering them to the destination failed. Treat the range as not done and start it again once the error is dealt with.
  • failed: the run ended without reaching delivery, or hit an internal error. Read error, fix the cause, such as a source that needs reconnecting, and run the range again.

Running a range again is safe for etlmcp's stored data: each run replaces the source's stored rows for exactly the days it covers, in one step, so a re-run never doubles them. Keep a list of the ranges that did not end as success and run them again at the end, rather than stopping the whole backfill on the first one.

The request and response fields are on the Start a run page, and the full API is described in the OpenAPI document.

Related