Using immudb Vault to Store JSON Files Securely for Application Development

JSON (JavaScript Object Notation) is a widely used data format for storing and exchanging data between a server and a ...


JSON (JavaScript Object Notation) is a widely used data format for storing and exchanging data between a server and a client. It is easy to read and write, making it a popular choice for application development. In this blog post, we will explore how to use JSONBin to store JSON documents for application development.

immudb Vault is a simple and powerful online JSON storage service that allows you to store, manage, and retrieve JSON documents securely, and with a tamper-proof guarantee. It provides a RESTful API that makes it easy to interact with the service programmatically.

fly-d-C5pXRFEjq3w-unsplash

To get started with immudb Vault, you will need to sign up for an account on the website.  Once you have an account, you can create a new vault, which is essentially a container for your JSON documents. Each vault has a unique ID that you can use to access and manipulate the data within it.

To store a JSON document in immudb Vault, you can make a POST request to the API endpoint https://vault.immudb.io/ics/api/v1/ledger/default/collection/default/document with the following payload:

{
    "key1": "value1",
    "key2": "value2",
}

Replace `key1`, `key2`, etc. with the keys and values of your JSON document. The API will respond with a JSON object containing the ID of the newly created vault.

As an example (the API key has been redacted):

curl -X 'PUT'   'https://vault.immudb.io/ics/api/v1/ledger/default/collection/default/document' \
  -H 'accept: application/json' \
  -H 'X-API-Key: [api_key]' \
  -H 'Content-Type: application/json' \
  -d '{
  "name": "John Doe",
  "id": 1,
  "timestamp": "2023-05-10T12:00:00Z",
  "email": "johndoe@example.com",
  "age": 30,
  "address": "123 Main Street",
  "city": "New York",
  "country": "USA",
  "phone": "+1-123-456-7890",
  "is_active": true
}'

For later examples, we will use 64cd03a70000000000000017085822bf as the document ID.

To retrieve a JSON document from immudb Vault, you can make a POST request to the API endpoint `https://vault.immudb.io/ics/api/v1/ledger/default/collection/default/document/<document_id>/audit` with following payload:

{
   "desc": true, 
   "page": 1, 
   "perPage":1
}'


Replace `{document_id}` with the ID of the document you want to retrieve. The API will respond with the JSON document stored in the vault, including its revision number.

As an example:

curl -X 'POST'  'https://vault.immudb.io/ics/api/v1/ledger/default/collection/default/document/64cd03a70000000000000017085822bf/audit'  \
  -H 'accept: application/json'  \
  -H 'X-API-Key: [api_key]'  \
  -H 'Content-Type: application/json' \
  -d '{"desc": true, "page": 1, "perPage":1}'

Another way to retrieve documents is to make a  POST request to the API endpoint https://vault.immudb.io/ics/api/v1/ledger/default/collection/default/documents/search with the following payload:

{
  "query": {
    "expressions": [
      {
        "fieldComparisons": [
          {
            "field": "_id",
            "operator": "EQ",
            "value": "<document_id>"
          }
        ]
      }
    ]
  },
  "page": 1,
  "perPage": 1
}'


As an example:

curl -X 'POST'  'https://vault.immudb.io/ics/api/v1/ledger/default/collection/default/documents/search' \
  -H 'accept: application/json' \
  -H 'X-API-Key: [api_key]'  \
  -H 'Content-Type: application/json'  \
  -d '
{
  "query": {
    "expressions": [
      {
        "fieldComparisons": [
          {
            "field": "_id",
            "operator": "EQ",
            "value": "64cd03a70000000000000017085822bf"
          }
        ]
      }
    ]
  },
  "page": 1,
  "perPage": 1
}'

You can also search documents by their other fields.

As an example:

curl -X 'POST'  'https://vault.immudb.io/ics/api/v1/ledger/default/collection/default/documents/search' \
  -H 'accept: application/json' \
  -H 'X-API-Key: [api_key]'  \
  -H 'Content-Type: application/json'  \
  -d '
{
  "query": {
    "expressions": [
      {
        "fieldComparisons": [
          {
            "field": "email",
            "operator": "EQ",
            "value": "johndoe@example.com"
          }
        ]
      }
    ]
  },
  "page": 1,
  "perPage": 1
}'


To update a JSON document in immudb Vault, you can make a POST request to the API endpoint https://vault.immudb.io/ics/api/v1/ledger/default/collection/default/document with the following payload:

{
  "data": {
    "key1": "new_value1",
    "key2": "new_value2",
    ...
  }
}

Replace `key1`, `key2`, etc. with the keys and new values you want to update. The API will respond with a JSON object containing the updated JSON document.

As an example:

curl -X 'POST' 'https://vault.immudb.io/ics/api/v1/ledger/default/collection/default/document' \
  -H 'accept: application/json' \
  -H 'X-API-Key: [api_key]' \
  -H 'Content-Type: application/json' \
  -d '
{
  "document": {
  "address": "123 Main Street",
  "age": 30,
  "city": "New York",
  "country": "USA",
  "email": "johndoe@example.com",
  "id": 1,
  "is_active": true,
  "name": "John Doe",
  "phone": "+1-123-456-7890",
  "timestamp": "2023-05-10T12:00:00Z"
    },
    "query": {
      "expressions": [
        {
          "fieldComparisons": [
            {
              "field": "_id",
              "operator": "EQ",
              "value": "64cd03a70000000000000017085822bf"
            }
          ]
        }
      ]
    }
}'

immudb Vault also provides additional features such as versioning, access control, and data privacy. You can explore these features in the immudb Vault documentation to enhance the security and management of your JSON documents.

BASH Snippet Full Example

#!/bin/bash

apiKey='default.iFMsoCCYeIy-ZB3yV5QnGA.HkUv2kFDUMm0DPLHwNz-E3A1VzeXvbEDQKTirO5-lHGDtxNW'

docId=$(curl -X 'PUT'   'https://vault-dev.immudb.io/ics/api/v1/ledger/default/collection/default/document' \
  -H 'accept: application/json' \
  -H "X-API-Key: $apiKey" \
  -H 'Content-Type: application/json' \
  -d '{
  "name": "John Doe",
  "id": 1,
  "timestamp": "2023-05-10T12:00:00Z",
  "email": "johndoe@example.com",
  "age": 30,
  "address": "123 Main Street",
  "city": "New York",
  "country": "USA",
  "phone": "+1-123-456-7890",
  "is_active": true
}' | jq -r '.documentId')


echo "--------Audit----------"

curl -X 'POST'  "https://vault-dev.immudb.io/ics/api/v1/ledger/default/collection/default/document/$docId/audit"  \
  -H 'accept: application/json'  \
  -H "X-API-Key: $apiKey" \
  -H 'Content-Type: application/json' \
  -d '{"desc": true, "page": 1, "perPage":1}' | jq

echo "--------Search by documentId----------"

curl -X 'POST'  'https://vault-dev.immudb.io/ics/api/v1/ledger/default/collection/default/documents/search' \
  -H 'accept: application/json' \
  -H "X-API-Key: $apiKey" \
  -H 'Content-Type: application/json'  \
  -d '
{
  "query": {
    "expressions": [
      {
        "fieldComparisons": [
          {
            "field": "_id",
            "operator": "EQ",
            "value": "64cd03a70000000000000017085822bf"
          }
        ]
      }
    ]
  },
  "page": 1,
  "perPage": 1
}' | jq

echo "-------Search by email-----------"

curl -X 'POST'  'https://vault-dev.immudb.io/ics/api/v1/ledger/default/collection/default/documents/search' \
  -H 'accept: application/json' \
  -H "X-API-Key: $apiKey" \
  -H 'Content-Type: application/json'  \
  -d '
{
  "query": {
    "expressions": [
      {
        "fieldComparisons": [
          {
            "field": "email",
            "operator": "EQ",
            "value": "johndoe@example.com"
          }
        ]
      }
    ]
  },
  "page": 1,
  "perPage": 1
}' | jq

echo "-------Update contents by documentId-----------"

curl -X 'POST' 'https://vault-dev.immudb.io/ics/api/v1/ledger/default/collection/default/document' \
  -H 'accept: application/json' \
  -H "X-API-Key: $apiKey" \
  -H 'Content-Type: application/json' \
  -d '
{
  "document": {
  "address": "123 Main Street",
  "age": 30,
  "city": "New York",
  "country": "USA",
  "email": "johndoe@example.com",
  "id": 1,
  "is_active": true,
  "name": "John Doe",
  "phone": "+1-123-456-7890",
  "timestamp": "2023-05-10T12:00:00Z"
    },
    "query": {
      "expressions": [
        {
          "fieldComparisons": [
            {
              "field": "_id",
              "operator": "EQ",
              "value": "64cd03a70000000000000017085822bf"
            }
          ]
        }
      ]
    }
}' | jq

In conclusion, JSON (JavaScript Object Notation) stands as a versatile and widely adopted data format for facilitating seamless data interchange between servers and clients. Its inherent readability and ease of use make it a favored choice in application development. In this exploration, we've delved into the functionalities of immudb Vault—a potent and straightforward online JSON storage service. Not only does immudb Vault ensure secure and tamper-proof storage, but it also empowers developers with a RESTful API for effortless interaction. By signing up and creating vaults, users establish dedicated spaces for their JSON documents, each stamped with a unique identifier. The process of storing, retrieving, and updating JSON documents is made straightforward through API calls, demonstrating immudb Vault's potential to streamline the development journey. As a repository of data integrity, security, and efficiency, immudb Vault emerges as a reliable cornerstone in the realm of JSON document storage for application development.