Connect to GraphQL API

How to connect to GraphQL API endpoint

HTTPS

Without secret

curl --location --request POST 'endpoint' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"query($address: String!){\n  blockchain{\n    account(address:$address){\n      info{\n        balance(format:DEC)\n      }\n    }\n  }\n}","variables":{"address":"0:e17ac4e77f46626579c7c4fefe35286117384c5ccfc8745c9780cdf056c378bf"}}'

With secret

curl --location --request POST 'endpoint' \
--header 'Authorization: Basic OmM1NWY3Y2Q4YzZmZTRjNTBhMDRjOTM0ODE0NTg3OWRi' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"query{\n  blockchain{\n    blocks(workchain:-1, last:1){\n      edges{\n        node{\n          hash\n          seq_no\n        }\n      }\n    }\n  }\n}","variables":{}}'

WSS

Without secret

const {TonClient} = require("@eversdk/core");
const {libNode} = require("@eversdk/lib-node");

TonClient.useBinaryLibrary(libNode)

const client = new TonClient({
    network: {
        endpoints: [
            "endpoint"
        ],
    },
});

async function _callback(response, responseType){
       /*
         * Where responseType:
         * 100 - GraphQL data received
         * 101 - GraphQL error received
         */

        if (responseType === 100) {
            if (response.result) {
                console.log("New block seq_no: "+ response.result.blocks.seq_no);

            }
        } else {
            console.log("Subscription failed with result: "+ JSON.stringify(response))
        }
    }

(async () => {
    try {
        subscriptionString = `
            subscription{
                blocks(filter:{
                workchain_id:{
                    eq:-1
                }
                }){
                seq_no
                id
                }
            }
        `
        const subscriptionHandler =  await client.net.subscribe({ 
            "subscription": subscriptionString }, _callback);        
        
        await new Promise(r => setTimeout(r, 10000));

        await client.net.unsubscribe(subscriptionHandler);

            
        client.close();
}
    catch (error) {
            console.error(error);
    }
}
)()

In the next section find out how to work with GraphQL Web playground and easily explore blockchain data with it.

Last updated

Was this helpful?