Download an export's file
#!/usr/bin/env bashset -euo pipefailAPI=https://api.speccapture.comAUTH="Authorization: Bearer $SPECCAPTURE_API_KEY"
EXPORT_ID=5f2a8c1e-3d4b-4e6f-9a7c-8b1d2e3f4a5b # the exportId a large export answered with
while true; do exportJob=$(curl -sS --fail-with-body "$API/v1/exports/$EXPORT_ID" \ -H "$AUTH") || { echo "$exportJob" >&2; exit 1; } jq -r '"Export \(.exportId) is \(.status)"' <<<"$exportJob" if [[ "$(jq -r '.status' <<<"$exportJob")" =~ ^(ready|failed|expired)$ ]]; then break; fi sleep 10done
curl -sS --fail-with-body "$API/v1/exports/$EXPORT_ID/file" \ -H "$AUTH" \ -o "$(jq -r '.fileName' <<<"$exportJob")"jq -r '"Saved \(.fileName) (\(.fileSizeBytes) bytes)"' <<<"$exportJob"using System.Text.Json;
using HttpClient http = new() { BaseAddress = new Uri("https://api.speccapture.com") };http.DefaultRequestHeaders.Authorization = new("Bearer", Environment.GetEnvironmentVariable("SPECCAPTURE_API_KEY"));
string exportId = args[0]; // the exportId a large export answered with
JsonElement exportJob;while (true) { exportJob = await Json(await Call(HttpMethod.Get, $"/v1/exports/{exportId}")); Console.WriteLine($"Export {exportJob.GetProperty("exportId")} is {exportJob.GetProperty("status")}"); if (exportJob.GetProperty("status").GetString() is "ready" or "failed" or "expired") break; await Task.Delay(TimeSpan.FromSeconds(10));}
HttpResponseMessage fileDownload = await Call(HttpMethod.Get, $"/v1/exports/{exportId}/file");await File.WriteAllBytesAsync(exportJob.GetProperty("fileName").GetString()!, await fileDownload.Content.ReadAsByteArrayAsync());Console.WriteLine($"Saved {exportJob.GetProperty("fileName")} ({exportJob.GetProperty("fileSizeBytes")} bytes)");
// Sends a request; stops with the problem's code and detail if the API refuses it.async Task<HttpResponseMessage> Call(HttpMethod method, string path, HttpContent? body = null, string? idempotencyKey = null) { using HttpRequestMessage request = new(method, path) { Content = body }; if (idempotencyKey is not null) request.Headers.Add("Idempotency-Key", idempotencyKey); HttpResponseMessage response = await http.SendAsync(request); if (!response.IsSuccessStatusCode) { JsonElement problem = await Json(response); string? detail = (problem.TryGetProperty("detail", out JsonElement text) ? text : problem.GetProperty("title")).GetString(); Console.Error.WriteLine($"{(int)response.StatusCode} {problem.GetProperty("code")}: {detail}"); Environment.Exit(1); } return response;}
// Reads an answer as JSON.static async Task<JsonElement> Json(HttpResponseMessage response) => JsonDocument.Parse(await response.Content.ReadAsStringAsync()).RootElement;import osimport sysimport time
import requests
API = "https://api.speccapture.com"session = requests.Session()session.headers["Authorization"] = f"Bearer {os.environ['SPECCAPTURE_API_KEY']}"
def call(method, path, **kwargs): """Send a request; stop with the problem's code and detail if the API refuses it.""" response = session.request(method, API + path, **kwargs) if not response.ok: problem = response.json() sys.exit(f"{response.status_code} {problem['code']}: {problem.get('detail', problem['title'])}") return response
export_id = sys.argv[1] # the exportId a large export answered with
while True: export_job = call("GET", f"/v1/exports/{export_id}").json() print(f"Export {export_job['exportId']} is {export_job['status']}") if export_job["status"] in ("ready", "failed", "expired"): break time.sleep(10)
with open(export_job["fileName"], "wb") as download: download.write(call("GET", f"/v1/exports/{export_id}/file").content)print(f"Saved {export_job['fileName']} ({export_job['fileSizeBytes']} bytes)")import { writeFile } from "node:fs/promises";import { setTimeout as sleep } from "node:timers/promises";
const API = "https://api.speccapture.com";const headers = { Authorization: `Bearer ${process.env.SPECCAPTURE_API_KEY}` };
const [exportId] = process.argv.slice(2); // the exportId a large export answered with
let exportJob;while (true) { exportJob = await call("GET", `/v1/exports/${exportId}`).then((response) => response.json()); console.log(`Export ${exportJob.exportId} is ${exportJob.status}`); if (["ready", "failed", "expired"].includes(exportJob.status)) break; await sleep(10000);}
const fileDownload = await call("GET", `/v1/exports/${exportId}/file`);await writeFile(exportJob.fileName, Buffer.from(await fileDownload.arrayBuffer()));console.log(`Saved ${exportJob.fileName} (${exportJob.fileSizeBytes} bytes)`);
// Sends a request; stops with the problem's code and detail if the API refuses it.async function call(method, path, init = {}) { const response = await fetch(API + path, { ...init, method, headers: { ...headers, ...init.headers } }); if (!response.ok) { const problem = await response.json(); console.error(`${response.status} ${problem.code}: ${problem.detail ?? problem.title}`); process.exit(1); } return response;}The ready export’s file, the same file a smaller export answers at once: json, a zip of csv files, or an xlsx workbook, saved as its fileName. Kept 7 days after it was ready. An export still queued or building, or one that failed, is 409 export_not_ready with exportStatus; one past its 7 days is 410 export_expired - ask for it again. Another workspace’s export is 404 not_found. Any live key.
Authorizations
Section titled “Authorizations”Parameters
Section titled “Parameters”Path Parameters
Section titled “Path Parameters”The export’s id: the exportId POST /v1/exports answered 202 with, or one GET /v1/exports lists.
Responses
Section titled “Responses”OK
The file’s bytes: save them as they come, under the name the Content-Disposition header gives.
The file’s bytes: save them as they come, under the name the Content-Disposition header gives.
The file’s bytes: save them as they come, under the name the Content-Disposition header gives.
Not Found: a problem with a stable code. See Errors for every code and what to do about it.
Every error: a problem with a stable code. https://speccapture.com/docs/api/errors lists every code.
object
A link to the code’s entry in the docs.
What went wrong, in a few words for a person: the same for every problem with this code.
The HTTP status of the answer.
What went wrong with this request and what to do about it, in a sentence or two for a person. Present on nearly every problem.
The path of the request that was refused.
What went wrong, as a stable code to branch on: a published code never changes meaning.
This request’s trace: quote it when asking for help.
Examples
Not found.
{ "type": "https://speccapture.com/docs/api/errors#not_found", "title": "Not found.", "status": 404, "detail": "There is no export with this id in your workspace.", "instance": "/v1/exports/5f2a8c1e-3d4b-4e6f-9a7c-8b1d2e3f4a5b/file", "code": "not_found", "traceId": "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"}Conflict: a problem with a stable code. See Errors for every code and what to do about it.
Every error: a problem with a stable code. https://speccapture.com/docs/api/errors lists every code.
object
A link to the code’s entry in the docs.
What went wrong, in a few words for a person: the same for every problem with this code.
The HTTP status of the answer.
What went wrong with this request and what to do about it, in a sentence or two for a person. Present on nearly every problem.
The path of the request that was refused.
What went wrong, as a stable code to branch on: a published code never changes meaning.
This request’s trace: quote it when asking for help.
Examples
The export's file is not ready.
{ "type": "https://speccapture.com/docs/api/errors#export_not_ready", "title": "The export's file is not ready.", "status": 409, "detail": "This export is still building: follow GET /v1/exports/5f2a8c1e-3d4b-4e6f-9a7c-8b1d2e3f4a5b, or wait for the export.ready webhook, then download it.", "instance": "/v1/exports/5f2a8c1e-3d4b-4e6f-9a7c-8b1d2e3f4a5b/file", "code": "export_not_ready", "traceId": "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01", "exportStatus": "building"}Gone: a problem with a stable code. See Errors for every code and what to do about it.
Every error: a problem with a stable code. https://speccapture.com/docs/api/errors lists every code.
object
A link to the code’s entry in the docs.
What went wrong, in a few words for a person: the same for every problem with this code.
The HTTP status of the answer.
What went wrong with this request and what to do about it, in a sentence or two for a person. Present on nearly every problem.
The path of the request that was refused.
What went wrong, as a stable code to branch on: a published code never changes meaning.
This request’s trace: quote it when asking for help.
Examples
The export's file has expired.
{ "type": "https://speccapture.com/docs/api/errors#export_expired", "title": "The export's file has expired.", "status": 410, "detail": "This export's file was kept for 7 days and has been deleted. Ask for the export again: POST /v1/exports.", "instance": "/v1/exports/5f2a8c1e-3d4b-4e6f-9a7c-8b1d2e3f4a5b/file", "code": "export_expired", "traceId": "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01", "exportStatus": "expired"}