/extract/segments/from-file

Base URL https://api.babylon.app/v1

POST /extract/segments/from-file

Extract one or more segment entries from a supported file. The file is sent as Base64 data in a nested data object.

JSON request body

  • segmentLedger (optional) — segment identifier supplied to tabular extraction.
  • accountAlias (optional) — accepted by the request contract, but not currently applied by the extraction service.
  • data (required) — file payload containing:
    • fileName (required) — file name including its extension.
    • mimeType (required) — media type describing the file.
    • sizeBytes (required) — positive size of the decoded file in bytes.
    • dataBase64 (required) — complete file contents encoded as Base64 without a data-URL prefix.
  • format (optional) — set to csv for a CSV response; otherwise the response is JSON.

The service recognises PDF, CSV, TSV, XLS, XLSX and EML input from the file content and name. Common media types include:

  • application/pdf
  • text/csv
  • text/tab-separated-values
  • application/vnd.ms-excel
  • application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  • message/rfc822

The request parser also accepts a columns field, but the current extraction service does not apply it to the response.

Example request

POST /extract/segments/from-file
Content-Type: application/json
{
  "segmentLedger": "MyGBSegment",
  "data": {
    "fileName": "contract_note.pdf",
    "mimeType": "application/pdf",
    "sizeBytes": 24576,
    "dataBase64": "JVBERi0xLjQKJc..."
  }
}

Example response — 200

The columns depend on the source document. A successful extraction returns the normal row-oriented table envelope:

{
  "name": "Segment Ledger Entry",
  "description": "",
  "columns": [
    "segmentLedger",
    "accountAlias",
    "tradeDate",
    "settleDate",
    "bourse",
    "type",
    "quantity",
    "symbol",
    "netAmount",
    "currency"
  ],
  "columnTypes": {
    "quantity": "Decimal",
    "netAmount": "Decimal"
  },
  "rows": [
    {
      "segmentLedger": "MyGBSegment",
      "accountAlias": "AJBell-GIA",
      "tradeDate": "2026-07-20",
      "settleDate": "2026-07-22",
      "bourse": "LSE",
      "type": "Buy",
      "quantity": "100",
      "symbol": "VEVE",
      "netAmount": "-10000",
      "currency": "GBP"
    }
  ]
}

You can request CSV either with "format": "csv" in the outer JSON object or with Accept: text/csv.

Python

import base64
import requests

API_URL = "https://api.babylon.app/v1/extract/segments/from-file"
FILE_PATH = "contract_note.pdf"

with open(FILE_PATH, "rb") as file:
    file_bytes = file.read()

payload = {
    "data": {
        "fileName": "contract_note.pdf",
        "mimeType": "application/pdf",
        "sizeBytes": len(file_bytes),
        "dataBase64": base64.b64encode(file_bytes).decode("ascii"),
    }
}

response = requests.post(API_URL, json=payload)
response.raise_for_status()

print(response.json())

Browser JavaScript

function fileToBase64(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();

    reader.onload = () => {
      if (typeof reader.result !== "string") {
        reject(new Error("Unexpected file result"));
        return;
      }

      const comma = reader.result.indexOf(",");

      if (comma < 0) {
        reject(new Error("Invalid data URL"));
        return;
      }

      resolve(reader.result.slice(comma + 1));
    };

    reader.onerror = () => {
      reject(reader.error ?? new Error("Unable to read file"));
    };

    reader.readAsDataURL(file);
  });
}

async function extractSegments(file) {
  const payload = {
    data: {
      fileName: file.name,
      mimeType: file.type || "application/octet-stream",
      sizeBytes: file.size,
      dataBase64: await fileToBase64(file)
    }
  };

  const response = await fetch(
    "https://api.babylon.app/v1/extract/segments/from-file",
    {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(payload)
    }
  );

  if (!response.ok) {
    throw new Error(`Extraction failed with status ${response.status}`);
  }

  return response.json();
}

There is no corresponding documented GET method.

Further reading

A Semantic Inference Engine for Tabular Data describes how Babylon interprets differently structured tabular records.

← Back to API