mintBlue
GuidesTransactions

Submit a transaction

Publish a first record with the SDK, then read it back and confirm the value that came out is the value you put in.

A transaction is one submission. It carries one or more outputs, and each output is one thing you are publishing. This guide submits a transaction with a single data output and then reads it back, so you finish holding evidence rather than a success message.

Before you start

You need three things, all from the Quickstart:

  • an SDK access token, created in the console under Access Tokens
  • a project, created under Projects, and its id
  • the package installed in an initialised Node project
npm install @mintblue/sdk

The samples on this page use require, which selects the package's CommonJS entry. SDK explains why that matters on a clean install and what to do if your project needs the ESM entry instead.

Step 1: Create a client

Mintblue.create takes your SDK access token and returns a client with your keys already unwrapped in memory. Read the token from the environment rather than from a literal in the file, so a copied snippet cannot carry a live credential into a repository.

const { Mintblue } = require('@mintblue/sdk');

async function main() {
  const client = await Mintblue.create({
    token: process.env.MINTBLUE_SDK_TOKEN,
  });
}

main();

Step 2: Describe what to publish

outputs is an array. Each entry names a type and the fields that type takes. A data output takes a value, and sign and encrypt decide whether the value is signed with your key and encrypted before it leaves your machine. Both default to off.

const outputs = [
  {
    type: 'data',
    value: 'invoice INV-0001 approved',
    sign: true,
    encrypt: true,
  },
];

Output types lists every shape outputs accepts.

Step 3: Submit it

createTransaction takes the project id and the outputs, and returns the transaction id along with the raw transaction.

const { txid } = await client.createTransaction({
  project_id: process.env.MINTBLUE_PROJECT_ID,
  outputs,
});

console.log('submitted', txid);

Step 4: Check it worked

A transaction id printed to your terminal proves the call returned. It does not prove the record carries what you meant. Fetch it back with parse: true, which decrypts the outputs your account is entitled to read and hands them back as objects.

const transaction = await client.getTransaction({ txid, parse: true });

const record = transaction.outputs.find((output) => output.type === 'data');
console.log(record.value);

If the value printed is the string you submitted, the round trip worked: the value was encrypted on your machine, published, fetched back and decrypted locally with your own key.

Match on type rather than on array position. The outputs you read back are not guaranteed to be only the outputs you submitted, and position is the first thing to break when you add a second output.

The whole script

const { Mintblue } = require('@mintblue/sdk');

async function main() {
  const client = await Mintblue.create({
    token: process.env.MINTBLUE_SDK_TOKEN,
  });

  const outputs = [
    {
      type: 'data',
      value: 'invoice INV-0001 approved',
      sign: true,
      encrypt: true,
    },
  ];

  const { txid } = await client.createTransaction({
    project_id: process.env.MINTBLUE_PROJECT_ID,
    outputs,
  });
  console.log('submitted', txid);

  const transaction = await client.getTransaction({ txid, parse: true });
  const record = transaction.outputs.find((output) => output.type === 'data');
  console.log('read back', record.value);
}

main();

Run it with the two values in the environment:

MINTBLUE_SDK_TOKEN=... MINTBLUE_PROJECT_ID=... node submit.js

You can also open the transaction in the console under your project, where the outputs are shown decrypted with your keys. The console tour is on mintBlue console.

Next

On this page