Skip to main content

JavaScript | Node.js

This example demonstrates using the truestamp-js package with Node.js and Promises.

Setup

npm install @truestamp/truestamp-js

Code Sample

This sample usage of the SDK will:

  • create the SHA-256 hash of the string "Truestamp rocks!"
  • submit the hash, hashType and description to the Truestamp API
truestamp-node-example.js
const crypto = require("crypto")

const Truestamp = require("@truestamp/truestamp-js")

// Create a new instance of the Truestamp SDK client, passing in your API key
const tsc = new Truestamp({ accessToken: "<<YOUR_API_KEY>>" })

/**
* @param {string} msg - The message to be hashed and submitted to the Truestamp API
* @param {string} desc - A description of the hash
*/
const createDocWithMessage = async (msg, desc) => {
let newDoc

// Create a new hash of the message
const msgHash = crypto.createHash("sha256").update(msg).digest("hex")

try {
newDoc = await tsc.createDocument({
hash: msgHash,
hashType: "sha2-256",
description: desc,
})
} catch (error) {
console.error(error)
}

return newDoc
}

// Call the async function to submit the hash to the Truestamp API
createDocWithMessage("Truestamp rocks!", "Describing my favorite company.")
.then((doc) => {
console.log(JSON.stringify(doc, null, 2))
})
.catch((error) => {
console.error(error)
})

Sample Response

The id property of the response will be the ID of the document created. This will be used to retrieve the commitment later.

All other properties are only for developer convenience.

{
"id": "T1201FG52BJX12G8AGMG5YPN9EY8E_0",
"hashBase64": "IGwob7ouJoaTIqrLy7tpVAlosBemAAOcatkRG/mbouE=",
"hashHex": "206c286fba2e26869322aacbcbbb69540968b017a600039c6ad9111bf99ba2e1",
"hashType": "sha2-256",
"description": "Describing my favorite company.",
"timestamp": "2021-09-21T20:57:04.402+00:00"
}