Examples

All HTTP requests have to be sent with the Content-Type: application/vnd.api+json header. If the header is not present, it will result in malformed responses or request rejections.

1const axios = require('axios') // install using npm: `npm install axios`
2const url = "http://0.0.0.0:8080" // http://${NODE_IP}:${JSON-RPC_PORT}
3const headers = {
4 "Content-Type": "application/json"
5}
6 
7const body = {} // this object is unique to each method described below
8 
9axios.post(url, body, headers)
10 .then(response => {
11 console.log(response.result)
12 }
13 .catch(error => {
14 console.log(error)
15 })
1package main
2 
3import (
4 "bytes"
5 "encoding/json"
6 "net/http"
7)
8 
9func post(URL string, body interface{}) (*http.Response, error) {
10 b := new(bytes.Buffer)
11 err := json.NewEncoder(b).Encode(u)
12 if err != nil {
13 return nil, err
14 }
15 return http.Post(URL, "application/json", b)
16}
17 
18type request struct {
19 Jsonrpc string `json:"jsonrpc"`
20 Method string `json:"method"`
21 ID int `json:"id"`
22 Params interface{} `json:"params"`
23}
24 
25func main() {
26 resp, err := post(
27 "http://0.0.0.0:8080",
28 request{},
29 )
30}
1import requests
2 
3r = requests.post("http://0.0.0.0:8080")
4print(r)

To complete the template, replace the empty body object with the objects provided in each quick action. The blocks.latest method, for example, can be accessed by the following script:

1const axios = require('axios') // install from npm with `npm install axios`
2const url = "http://0.0.0.0:8080" // http://${NODE_ID}:${JSON-RPC-PORT}
3const headers = {
4 "Content-Type": "application/json"
5}
6 
7const body = {
8 jsonrpc: "2.0", // JSON-RPC API version.
9 method: "blocks.latest", // RPC method.
10 id: 31 // internal ID to track responses.
11}
12 
13axios.post(url, body, headers)
14 .then(response => {
15 console.log(response.data)
16 }
17 .catch(error => {
18 console.log(error)
19 })
1package main
2 
3import (
4 "bytes"
5 "encoding/json"
6 "net/http"
7)
8 
9func post(URL string, body interface{}) (*http.Response, error) {
10 b := new(bytes.Buffer)
11 err := json.NewEncoder(b).Encode(u)
12 if err != nil {
13 return nil, err
14 }
15 return http.Post(URL, "application/json; charset=utf-8", b)
16}
17 
18type request struct {
19 Jsonrpc string `json:"jsonrpc"`
20 Method string `json:"method"`
21 ID int `json:"id"`
22 Params interface{} `json:"params"`
23}
24 
25func main() {
26 resp, err := post(
27 "http://0.0.0.0:8080",
28 request{
29 Jsonrpc: "2.0",
30 Method: "blocks.latest",
31 ID: 31,
32 Params: nil,
33 },
34 )
35}
1import requests
2r = requests.post(
3 "http://0.0.0.0:8080",
4 json={"jsonrpc": "2.0", "method": "blocks.latest", "id": 31}
5 )
6print(r)

Check Wallet Balance

This method can be used to check the account balance associated with a particular ARK address. To utilize it, use the following body payload:

1const body = {
2 jsonrpc: "2.0",
3 method: "wallets.info",
4 id: 31 // internal ID to track responses
5 params: {
6 address: "AMv3iLrvyvpi6d4wEfLqX8kzMxaRvxAcHT" // the address of the wallet being queried.
7 }
8}
1package main
2 
3...
4 
5type walletInfoParams struct {
6 Address string `json:"address"`
7}
8 
9func main() {
10 resp, err := post(
11 "http://0.0.0.0:8080",
12 request{
13 Jsonrpc: "2.0",
14 Method: "wallets.info",
15 ID: 31,
16 Params: walletInfoParams{
17 Address: "AMv3iLrvyvpi6d4wEfLqX8kzMxaRvxAcHT",
18 },
19 },
20 )
21}
1r = requests.post(
2 "http://0.0.0.0:8080",
3 json={"jsonrpc": "2.0", "method": "wallets.info", "id": 31,
4 "params": {
5 "address": "AMv3iLrvyvpi6d4wEfLqX8kzMxaRvxAcHT",
6 },
7 }
8 )

The response will contain the jsonrpc and id you used to call the request, along with a payload containing the following data:

1{
2 "address": "AMv3iLrvyvpi6d4wEfLqX8kzMxaRvxAcHT",
3 "balance": 245098210000000,
4 "isDelegate": true,
5 "publicKey": "02532c68cd0842fb86b2202c1027eafc741bdd581517047d9d19319e6741c54883",
6 "secondPublicKey": null,
7 "username": "genesis_30"
8}

Find Block Information

If you want to retrieve the latest block on the blockchain, call the blocks.latest method with no parameters:

1const body = {
2 jsonrpc: "2.0",
3 method: "blocks.latest",
4 id: 31 // internal ID to track responses
5};
1package main
2 
3...
4 
5func main() {
6 resp, err := post(
7 "http://0.0.0.0:8080",
8 request{
9 Jsonrpc: "2.0",
10 Method: "blocks.latest",
11 ID: 31,
12 Params: nil,
13 },
14 )
15}
1r = requests.post(
2 "http://0.0.0.0:8080",
3 json={"jsonrpc": "2.0", "method": "blocks.latest", "id": 31}
4 )

This returns a response similar to the following:

1{
2 "forged": {
3 "amount": 0,
4 "fee": 0,
5 "reward": 0,
6 "total": 0
7 },
8 "generator": {
9 "address": "AdWRsk7Lbo97jxGBKzLAFwevVHbqVbW1Cj",
10 "publicKey": "03691178f8610d0a295e650201b62345056c788d7f9ac7e8570b69c6c90091b564",
11 "username": "genesis_8"
12 },
13 "height": 20582,
14 "id": "5897025410627682852",
15 "payload": {
16 "hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
17 "length": 0
18 },
19 "previous": "9643009166535029717",
20 "signature": "30440220772362881112eb0ce65d2a91b92cbb6b404f83165edfc95aa2cfb19a02026a3a022010bec681e7b9abfca61a4961f0e29db6730e8d3f9c649b5ab4b7eee1b919897e",
21 "timestamp": {
22 "epoch": 54902770,
23 "human": "2018-12-16T23:46:10.000Z",
24 "unix": 1545003970
25 },
26 "transactions": 0,
27 "version": 0
28}

Create and Broadcast Transactions

Creating a transaction using the JSON-RPC is a two-step process:

  1. Create the transaction object with transactions.create.
  2. Broadcast the transaction to the network with transactions.broadcast.

The transactions.create endpoint accepts three parameters:

  • RecipientId
  • Amount
  • Passphrase

An example transaction creation payload could look like this:

1const body = {
2 jsonrpc: "2.0",
3 method: "transactions.create",
4 id: 31
5 params: {
6 recipientId: "AMv3iLrvyvpi6d4wEfLqX8kzMxaRvxAcHT" // the address you want to send to,
7 amount: "200000000", // 2 ARK * 100,000,000 arktoshi/ARK
8 passphrase: "craft imitate step mixture patch forest volcano business charge around girl confirm"
9 }
10}
1package main
2 
3...
4 
5type transaction struct {
6 RecipientId string `json:"recipientId"`
7 Amount string `json:"amount"`
8 Passphrase string `json:"passphrase"`
9}
10 
11func main() {
12 resp, err := post(
13 "http://0.0.0.0:8080",
14 request{
15 Jsonrpc: "2.0",
16 Method: "transactions.create",
17 ID: 31,
18 Params: transaction{
19 Address: "AMv3iLrvyvpi6d4wEfLqX8kzMxaRvxAcHT",
20 Amount: "200000000", // 2 ARK * 100,000,000 arktoshi/ARK
21 Passphrase: "craft imitate step mixture patch forest volcano business charge around girl confirm",
22 },
23 },
24 )
25}
1r = requests.post(
2 "http://0.0.0.0:8080",
3 json={"jsonrpc": "2.0", "method": "transactions.create", "id": 31,
4 "params": {
5 "recipientId": "AMv3iLrvyvpi6d4wEfLqX8kzMxaRvxAcHT" # the address you want to send to,
6 "amount": "200000000", # 2 ARK * 100,000,000 arktoshi/ARK
7 "passphrase": "craft imitate step mixture patch forest volcano business charge around girl confirm"
8 },
9 }
10 )

This endpoint will return a transaction object similar to the following:

1{
2 "amount": "200000000",
3 "fee": 10000000,
4 "id": "b60525042509586151fac7e3c70fe7a75ca00ffdf9988f20d0c1c0f3db798e86",
5 "recipientId": "AMv3iLrvyvpi6d4wEfLqX8kzMxaRvxAcHT",
6 "senderPublicKey": "038082dad560a22ea003022015e3136b21ef1ffd9f2fd50049026cbe8e2258ca17",
7 "signature": "304402204236a59a19266b5969e18f87d6d4b178180277c79beb5d4b42f272ee03fba0b702200c6c97ed5ab2e6231f3dce5cdfe740e72261b460f896fb4c5be0ca7ce6244c67",
8 "timestamp": 54903765,
9 "type": 0
10}

Importantly, this does not mean your transaction has been added to the blockchain! To do so, we’ll need to submit a second request to transactions.broadcast.

This request should have a params object with a single key: the id key returned by transactions.create.

With the returned ID, our second request body looks like this:

1const body = {
2 jsonrpc: "2.0",
3 method: "transactions.broadcast",
4 id: 31
5 params: {
6 id: "b60525042509586151fac7e3c70fe7a75ca00ffdf9988f20d0c1c0f3db798e86"
7 }
8}
1package main
2 
3...
4 
5type broadcast struct {
6 Id string `json:"id"`
7}
8 
9func main() {
10 resp, err := post(
11 "http://0.0.0.0:8080",
12 request{
13 Jsonrpc: "2.0",
14 Method: "transactions.broadcast",
15 ID: 31,
16 Params: broadcast{
17 Id: "b60525042509586151fac7e3c70fe7a75ca00ffdf9988f20d0c1c0f3db798e86",
18 },
19 },
20 )
21}
1r = requests.post(
2 "http://0.0.0.0:8080",
3 json={"jsonrpc": "2.0", "method": "transactions.broadcast", "id": 31,
4 "params": {
5 "id": "b60525042509586151fac7e3c70fe7a75ca00ffdf9988f20d0c1c0f3db798e86",
6 },
7 }
8 )

If we receive the same transaction object as the call to transactions.create, our transaction was successful. Within your application, one way to confirm the result is to check that result.id matches the transaction ID you provided to the endpoint.

Otherwise, the errors key will contain more information on what went wrong.

Check Transaction Confirmations

Checking the number of confirmations a transaction can be done via JSON-RPC by the transactions.info method.

The command accepts one parameter: the id of the transaction to query. A sample request could look like:

1const body = {
2 jsonrpc: "2.0",
3 method: "transactions.info",
4 id: 9,
5 params: {
6 id: "b60525042509586151fac7e3c70fe7a75ca00ffdf9988f20d0c1c0f3db798e86"
7 }
8};
1package main
2 
3...
4 
5type transactionInfo struct {
6 Id string `json:"id"`
7}
8 
9func main() {
10 resp, err := post(
11 "http://0.0.0.0:8080",
12 request{
13 Jsonrpc: "2.0",
14 Method: "transactions.info",
15 ID: 31,
16 Params: transactionInfo{
17 id: "b60525042509586151fac7e3c70fe7a75ca00ffdf9988f20d0c1c0f3db798e86",
18 },
19 },
20 )
21}
1r = requests.post(
2 "http://0.0.0.0:8080",
3 json={"jsonrpc": "2.0", "method": "transactions.info", "id": 31,
4 "params": {
5 "id": "b60525042509586151fac7e3c70fe7a75ca00ffdf9988f20d0c1c0f3db798e86",
6 },
7 }
8 )

If successful, you’ll receive a response similar to the following:

1{
2 "amount": 200000000,
3 "blockId": "16888082711050311577",
4 "confirmations": 27,
5 "fee": 10000000,
6 "id": "b60525042509586151fac7e3c70fe7a75ca00ffdf9988f20d0c1c0f3db798e86",
7 "recipient": "AMv3iLrvyvpi6d4wEfLqX8kzMxaRvxAcHT",
8 "sender": "ARAibxGqLQJTo1bWMJfu5fCc88rdWWjqgv",
9 "signature": "304402204236a59a19266b5969e18f87d6d4b178180277c79beb5d4b42f272ee03fba0b702200c6c97ed5ab2e6231f3dce5cdfe740e72261b460f896fb4c5be0ca7ce6244c67",
10 "timestamp": {
11 "epoch": 54903765,
12 "human": "2018-12-17T00:02:45.000Z",
13 "unix": 1545004965
14 },
15 "type": 0,
16 "version": 1
17}

This particular transaction has 27 confirmations, meaning you can be confident that this transaction has been irreversibly included in the blockchain. Most exchanges use a minimum of 51 confirmations, which is one complete round.

Last updated 4 years ago
Edit Page
Share: