LLM-readable documentation index

Reuse Supplier Data Across Invoices

Read a supplier profile during extraction and save the invoice to Structured Memory

Hand off to an LLM

An invoice may leave out the currency when the supplier uses the same one for every order. Use Structured Memory to give Extract the supplier's default currency and save the completed invoice in the same bucket.

Prerequisites

  • A bem API key for a test environment, exported as BEM_API_KEY.
  • cURL to run the commands. The example provides the sample invoice.

Step 1: Create a bucket

Create one bucket for supplier supplier-1042.

curl -X POST https://api.bem.ai/v3/structured-memory/buckets \
  -H "Content-Type: application/json" \
  -H "x-api-key: $BEM_API_KEY" \
  -d '{"key": "supplier-1042", "displayName": "Supplier 1042"}'

A 201 response confirms the bucket was created.

Step 2: Create the kinds

Create a supplier-profile kind for the facts you keep about the supplier.

curl -X POST https://api.bem.ai/v3/structured-memory/kinds \
  -H "Content-Type: application/json" \
  -H "x-api-key: $BEM_API_KEY" \
  -d '{
    "name": "supplier-profile",
    "attributesSchema": {
      "type": "object",
      "properties": {
        "supplierName": { "type": "string" },
        "currency": { "type": "string" },
        "paymentTermsDays": { "type": "integer" }
      },
      "required": ["supplierName", "currency", "paymentTermsDays"]
    }
  }'

Create an invoice kind for the invoices the workflow saves. Use the same schema for this kind and the Extract function. Every field in it is required and doesn't allow null, so a Write fails for an invoice that leaves one out. To accept such invoices, see fields a document can leave out.

curl -X POST https://api.bem.ai/v3/structured-memory/kinds \
  -H "Content-Type: application/json" \
  -H "x-api-key: $BEM_API_KEY" \
  -d '{
    "name": "invoice",
    "attributesSchema": {
      "type": "object",
      "properties": {
        "invoiceNumber": { "type": "string" },
        "invoiceDate": { "type": "string" },
        "supplierName": { "type": "string" },
        "currency": {
          "type": "string",
          "description": "ISO 4217 currency code. When the invoice states no currency, use the currency of the supplier profile."
        },
        "totalAmount": { "type": "number" }
      },
      "required": ["invoiceNumber", "invoiceDate", "supplierName", "currency", "totalAmount"]
    }
  }'

Each request returns 201 and creates version 1 of the kind.

Step 3: Add the supplier profile

Save an entry with the supplier's default currency.

curl -X POST https://api.bem.ai/v3/structured-memory/buckets/supplier-1042/entries \
  -H "Content-Type: application/json" \
  -H "x-api-key: $BEM_API_KEY" \
  -d '{
    "kindName": "supplier-profile",
    "kindVersionNum": 1,
    "key": "profile",
    "attributes": {
      "supplierName": "Example Packaging Ltd.",
      "currency": "EUR",
      "paymentTermsDays": 30
    }
  }'

The response is 201 with the entry. If the request fails, check that the attributes match the supplier profile schema.

Step 4: Create the functions

Create a Read Memory function. It has no configuration.

curl -X POST https://api.bem.ai/v3/functions \
  -H "Content-Type: application/json" \
  -H "x-api-key: $BEM_API_KEY" \
  -d '{"functionName": "supplier-memory-read", "type": "read_memory"}'

Create the Extract function with the invoice schema from step 2.

curl -X POST https://api.bem.ai/v3/functions \
  -H "Content-Type: application/json" \
  -H "x-api-key: $BEM_API_KEY" \
  -d '{
    "functionName": "invoice-extract",
    "type": "extract",
    "outputSchemaName": "invoice",
    "outputSchema": {
      "type": "object",
      "properties": {
        "invoiceNumber": { "type": "string" },
        "invoiceDate": { "type": "string" },
        "supplierName": { "type": "string" },
        "currency": {
          "type": "string",
          "description": "ISO 4217 currency code. When the invoice states no currency, use the currency of the supplier profile."
        },
        "totalAmount": { "type": "number" }
      },
      "required": ["invoiceNumber", "invoiceDate", "supplierName", "currency", "totalAmount"]
    }
  }'

Create a Write Memory function that saves entries of invoice version 1.

curl -X POST https://api.bem.ai/v3/functions \
  -H "Content-Type: application/json" \
  -H "x-api-key: $BEM_API_KEY" \
  -d '{
    "functionName": "invoice-memory-write",
    "type": "write_memory",
    "writeMemoryConfig": { "kindName": "invoice", "kindVersionNum": 1 }
  }'

Step 5: Create the workflow

Set Extract as the main node. Connect Read Memory to Extract as context, then connect Extract to Write Memory.

curl -X POST https://api.bem.ai/v3/workflows \
  -H "Content-Type: application/json" \
  -H "x-api-key: $BEM_API_KEY" \
  -d '{
    "name": "supplier-invoices",
    "mainNodeName": "extract",
    "nodes": [
      { "name": "read", "function": { "name": "supplier-memory-read" } },
      { "name": "extract", "function": { "name": "invoice-extract" } },
      { "name": "write", "function": { "name": "invoice-memory-write" } }
    ],
    "edges": [
      { "sourceNodeName": "read", "destinationNodeName": "extract" },
      { "sourceNodeName": "extract", "destinationNodeName": "write" }
    ]
  }'

Step 6: Subscribe to saved entries (optional)

To receive each saved entry at your webhook URL, subscribe to the Write Memory function.

curl -X POST https://api.bem.ai/v3/subscriptions \
  -H "Content-Type: application/json" \
  -H "x-api-key: $BEM_API_KEY" \
  -d '{
    "name": "invoice-entries",
    "type": "write_memory",
    "functionName": "invoice-memory-write",
    "webhookURL": "https://your-app.example.com/webhooks/bem"
  }'

To verify deliveries, see Webhooks.

Step 7: Call the workflow

Save this invoice as invoice.txt. It states no currency.

INVOICE

Invoice number: INV-2041
Invoice date: 2026-11-20
From: Example Packaging Ltd.
Bill to: Example Retail Inc.

Description              Qty   Unit price     Amount
Packing tape, 48 mm       40         3.25     130.00
Shipping boxes, large    200         5.60   1,120.00

Total                                       1,250.00

Call the workflow with the supplier profile in memory.read and the new invoice entry in memory.write.

curl -X POST https://api.bem.ai/v3/workflows/supplier-invoices/call \
  -H "x-api-key: $BEM_API_KEY" \
  -F "wait=true" \
  -F "callReferenceID=INV-2041" \
  -F "file=@invoice.txt" \
  --form-string 'memory={"read": ["bem://memory/buckets/supplier-1042?kind=supplier-profile"], "write": "bem://memory/buckets/supplier-1042/entries/INV-2041"}'

In a multipart call, memory is a JSON object string. In a JSON call, memory is an object in the body.

{
  "input": {
    "singleFile": {
      "inputType": "text",
      "inputContent": "<base64 of invoice.txt>"
    }
  },
  "memory": {
    "read": ["bem://memory/buckets/supplier-1042?kind=supplier-profile"],
    "write": "bem://memory/buckets/supplier-1042/entries/INV-2041"
  }
}

Open call.outputs[0] to find the write_memory event. The saved entry includes the currency from the supplier profile. The response below omits unrelated fields.

{
  "call": {
    "callID": "wc_3Mw7Wss4M3p3Jiro2tpmdJyn6dr",
    "status": "completed",
    "workflowName": "supplier-invoices",
    "callReferenceID": "INV-2041",
    "outputs": [
      {
        "eventID": "evt_3Mw7X9PAp6lflibSgPOKNfXaeQY",
        "eventType": "write_memory",
        "functionName": "invoice-memory-write",
        "callID": "wc_3Mw7Wss4M3p3Jiro2tpmdJyn6dr",
        "entry": {
          "id": "ment_3Mw7X8EzIrLiKFmYeb1aTczcTd3",
          "key": "INV-2041",
          "bucketKey": "supplier-1042",
          "kindName": "invoice",
          "kindVersionNum": 1,
          "attributes": {
            "currency": "EUR",
            "invoiceDate": "2026-11-20",
            "totalAmount": 1250,
            "supplierName": "Example Packaging Ltd.",
            "invoiceNumber": "INV-2041"
          },
          "producingCallID": "wc_3Mw7Wss4M3p3Jiro2tpmdJyn6dr"
        }
      }
    ],
    "errors": []
  }
}

If the call is rejected, check that the bucket and kind exist and that both Memory URIs match the workflow. See Structured Memory request errors.

Step 8: Read the saved entry

Fetch the invoice from the supplier bucket.

curl https://api.bem.ai/v3/structured-memory/buckets/supplier-1042/kinds/invoice/entries/INV-2041 \
  -H "x-api-key: $BEM_API_KEY"

The response is the entry, with its createdAt:

{
  "id": "ment_3Mw7X8EzIrLiKFmYeb1aTczcTd3",
  "key": "INV-2041",
  "bucketKey": "supplier-1042",
  "kindName": "invoice",
  "kindVersionNum": 1,
  "attributes": {
    "currency": "EUR",
    "invoiceDate": "2026-11-20",
    "totalAmount": 1250,
    "supplierName": "Example Packaging Ltd.",
    "invoiceNumber": "INV-2041"
  },
  "producingCallID": "wc_3Mw7Wss4M3p3Jiro2tpmdJyn6dr",
  "createdAt": "2026-12-02T22:16:52.536433Z"
}

Step 9: Check which entries the call selected

Get the call trace and inspect trace.readCapture for the selectors and matching entries. The response below shows only readCapture.

curl https://api.bem.ai/v3/calls/wc_3Mw7Wss4M3p3Jiro2tpmdJyn6dr/trace \
  -H "x-api-key: $BEM_API_KEY"
{
  "trace": {
    "readCapture": {
      "selectors": ["bem://memory/buckets/supplier-1042?kind=supplier-profile"],
      "entries": [
        {
          "id": "ment_3Mw7U5psoXhQtQUZvUl091r8Dro",
          "key": "profile",
          "bucketKey": "supplier-1042",
          "kindName": "supplier-profile",
          "kindVersionNum": 1,
          "attributes": {
            "currency": "EUR",
            "supplierName": "Example Packaging Ltd.",
            "paymentTermsDays": 30
          }
        }
      ]
    }
  }
}

Verify webhook delivery

If you subscribed to the Write Memory function, compare the webhook body with outputs[0]. Both have the same eventID and entry. Use eventID to detect duplicate deliveries.

On this page