Skip to content

#103_Multichain_API

AhmadBAmzah edited this page May 2, 2019 · 10 revisions

Spike #103 - Multichain API

Ahmad Bin Amzah – 01/05/2019

Goals / Deliverables

  • Select the best language to use in an API
  • Deploying data stream with an API

Technologies, Tools and Resources Used

  • Multichain Documentation
  • Forums

What we found out

Multichain offers a JSON-RPC API that can be used to access and manage the blockchain remotely. There are several different methods to access and execute Multichain API calls. One of them involves using third-party libraries which provides convenient wrappers that enable applications to interact with Multichain. However, there are methods that do not require the use of third-party libraries.

Using POST to execute Multichain API calls

Multichain API calls can be sent via a POST request to the Multichain Blockchain which needs be done by any language that supports HTTP basic authentication. There are several requirements to use this method;

  1. Acquire the IP address of the node instance and the RPC port which is obtained via running the command multichain-cli [chain-name] getblockchainparams under "default-rpc-port".
  2. Acquire the RPC username and password obtained by reading the file multichain.conf in the ~/.multichain/[chain-name]/ directory.
  3. Allow RPC connections by adding the rpcallowip= setting to the multichain.conf and specifying the IP addresses permitted to make RPC connections to Multichain. Although it is advised not to use "0.0.0.0/0" to allow any address to make connections, for the purposes of testing, this can make it easier seeing as we are working on a prototype.
  4. After specifying the allowed IP addresses, the node needs to restart. Simply execute the following commands; multichain-cli [chain_name] stop, multichaind [chain_name] -daemon

After fulfilling the requirements, HTTP basic authentication with the RPC username and password to the appropiate IP address and RPC port could be used with an API to send JSON which will execute Multichain JSON-RPC calls listed here. JSON would be stored in the POST body in the format;

{"method":"create","params":["stream1","false"],"id":123,"chain_name":"chain1"}

This is the same as using the command-line to execute the API command multichain-cli chain1 create stream1 false, which will create a stream to the blockchain named "stream1".

Example - Retrieving Data Stream

  1. Using Postman to test and create POST requests, JSON could be sent to the RPC port (6836) and IP address of the node instance (43.240.97.10). The URL used would be http://43.240.97.10:6836/.
  2. An Authorization header would be generated by specifying the authorization type with Basic Authentication, entering the username (multichainrpc) and password obtained via multichain.conf.
  3. The Content-Type header is used to specify the type of data stored in the body, which is JSON.
  4. Within the body, the following JSON data is stored to execute an API call which will retrieve information about "stream1";
{"method":"getstreaminfo","params":["stream1"],"id":"123","chain_name":"chain1"}
  1. HTTP request:
POST / HTTP/1.1
Host: 43.240.97.10:6836
Content-Type: application/json
Authorization: Basic bXVsdGljaGFpbnJwYzpCU1lYa2dVOVRMWG9oMndzTnM1Q1JqODRiQzl1bmZ1QWZzNktDSkxZaEo5bw==
cache-control: no-cache
Postman-Token: db128892-1e9b-46fb-8cd3-28d06cae06c7

{"method":"getstreaminfo","params":["stream1"],"id":"123","chain_name":"chain1"}
  1. After sending the request, the HTTP response will return information about "stream1" within the message body as JSON data;
{
    "result": {
        "name": "stream1",
        "createtxid": "96041222c6d24fcf451aea905bcdfc1287d5238069284bef5e36f1af970e624f",
        "streamref": "7-264-1174",
        "restrict": {
            "write": true,
            "onchain": false,
            "offchain": false
        },
        "details": {}
    },
    "error": null,
    "id": "123"
}

Clone this wiki locally