Download a finished job's values as JSON, CSV or XLSX
#!/usr/bin/env bashset -euo pipefailAPI=https://api.speccapture.comAUTH="Authorization: Bearer $SPECCAPTURE_API_KEY"
JOB_ID=7c9e6679-7425-40de-944b-e07fc1f90ae7 # the jobId the submission answered with
curl -sS --fail-with-body "$API/v1/extractions/$JOB_ID/export?format=xlsx" \ -H "$AUTH" \ -o extraction.xlsxcurl -sS --fail-with-body "$API/v1/extractions/$JOB_ID/export?format=csv" \ -H "$AUTH" \ -o extraction-csv.zipcurl -sS --fail-with-body "$API/v1/extractions/$JOB_ID/export?format=json" \ -H "$AUTH" \ -o extraction.jsonecho "Saved extraction.xlsx, extraction-csv.zip and extraction.json"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 jobId = args[0]; // the jobId the submission answered with
HttpResponseMessage xlsxDownload = await Call(HttpMethod.Get, $"/v1/extractions/{jobId}/export?format=xlsx");await File.WriteAllBytesAsync("extraction.xlsx", await xlsxDownload.Content.ReadAsByteArrayAsync());HttpResponseMessage csvDownload = await Call(HttpMethod.Get, $"/v1/extractions/{jobId}/export?format=csv");await File.WriteAllBytesAsync("extraction-csv.zip", await csvDownload.Content.ReadAsByteArrayAsync());HttpResponseMessage jsonDownload = await Call(HttpMethod.Get, $"/v1/extractions/{jobId}/export?format=json");await File.WriteAllBytesAsync("extraction.json", await jsonDownload.Content.ReadAsByteArrayAsync());Console.WriteLine("Saved extraction.xlsx, extraction-csv.zip and extraction.json");
// 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 sys
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
job_id = sys.argv[1] # the jobId the submission answered with
with open("extraction.xlsx", "wb") as download: download.write(call("GET", f"/v1/extractions/{job_id}/export?format=xlsx").content)with open("extraction-csv.zip", "wb") as download: download.write(call("GET", f"/v1/extractions/{job_id}/export?format=csv").content)with open("extraction.json", "wb") as download: download.write(call("GET", f"/v1/extractions/{job_id}/export?format=json").content)print("Saved extraction.xlsx, extraction-csv.zip and extraction.json")import { writeFile } from "node:fs/promises";
const API = "https://api.speccapture.com";const headers = { Authorization: `Bearer ${process.env.SPECCAPTURE_API_KEY}` };
const [jobId] = process.argv.slice(2); // the jobId the submission answered with
const xlsxDownload = await call("GET", `/v1/extractions/${jobId}/export?format=xlsx`);await writeFile("extraction.xlsx", Buffer.from(await xlsxDownload.arrayBuffer()));const csvDownload = await call("GET", `/v1/extractions/${jobId}/export?format=csv`);await writeFile("extraction-csv.zip", Buffer.from(await csvDownload.arrayBuffer()));const jsonDownload = await call("GET", `/v1/extractions/${jobId}/export?format=json`);await writeFile("extraction.json", Buffer.from(await jsonDownload.arrayBuffer()));console.log("Saved extraction.xlsx, extraction-csv.zip and extraction.json");
// 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;}Format=json, csv (a zip: sheets.csv, one row per sheet, or values.csv, one row per value, and one file per table type) or xlsx (one workbook), each laid out as the job’s template’s output chooses: json is the template’s shape (as GET /v1/extractions/{jobId}/values gives it, with formatVersion, workspace and exportedAt) when the template lays its JSON out flat or nested, and the job and every sheet exactly as the API returns them when it is complete - the standard template’s, and every template’s that never chose; in csv and xlsx each value is a column, followed by the extras the template carries. content=complete asks for every detail of what the job kept instead, whatever its template’s files hold: json is then the job and every sheet exactly as the API returns them, and csv and xlsx carry what was read, the review state and the confidence beside every value and each sheet’s details; it never adds a field the job did not keep, and a value still waiting for a person comes with its state. keys=paths or keys=names chooses a template-shaped json file’s keys, as on GET /v1/extractions/{jobId}/values. Answers 409 not_finished until every sheet has finished.
Authorizations
Section titled “Authorizations”Parameters
Section titled “Parameters”Path Parameters
Section titled “Path Parameters”The job’s id: the jobId POST /v1/extractions answered with, or one GET /v1/extractions lists.
Query Parameters
Section titled “Query Parameters”The file to download: json (the job and every sheet exactly as this API answers them), csv (a zip of sheets.csv and one file per table) or xlsx (one Excel workbook). json when left out.
What the file holds: template, as the job’s template lays its files out, or complete, every detail of what the job kept whatever its template says - json the job and every sheet exactly as this API answers them, csv and xlsx with what was read, the review state and the confidence beside every value and each sheet’s details. Never a field the job did not keep; a value still waiting for a person comes with its state. Made afresh, never stored. template when left out.
What the values are keyed by: names, the template’s names, or paths, SpecCapture’s keys (titleBlock.partNumber, bom, qty), which a rename never changes. Changes a template-shaped answer or json file only: the complete answer carries both each value’s path and its name. The template’s own choice when left out.
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.
Bad Request: 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 request is not valid.
{ "type": "https://speccapture.com/docs/api/errors#invalid_request", "title": "The request is not valid.", "status": 400, "detail": "format must be json, csv or xlsx.", "instance": "/v1/extractions/7c9e6679-7425-40de-944b-e07fc1f90ae7/export", "code": "invalid_request", "traceId": "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"}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 extraction with this id in your workspace.", "instance": "/v1/extractions/7c9e6679-7425-40de-944b-e07fc1f90ae7/export", "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 job has not finished.
{ "type": "https://speccapture.com/docs/api/errors#not_finished", "title": "The job has not finished.", "status": 409, "detail": "The job is still extracting. Its export is ready once every sheet has finished: follow GET /v1/extractions/7c9e6679-7425-40de-944b-e07fc1f90ae7, or a webhook.", "instance": "/v1/extractions/7c9e6679-7425-40de-944b-e07fc1f90ae7/export", "code": "not_finished", "traceId": "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01", "jobStatus": "extracting"}