Skip to content

Latest commit

 

History

History

node-bcd

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

alt text

Hicetnunc / Better Call Dev API Guide

A guide for using the Better Call Dev API to query and parse hic et nunc data.

Better Call Dev

Better Call Dev is a public Tezos block explorer API created by Baking Bad. BCD has a public REST endpoint and returns data in a JSON object. We can use BCD to request data on OBJKTs and tz addresses.

Rate Limiting

One of the things you need to consider when sending REST messages is to not send too many outgoing messages at once. This can cause the REST server to send an error message. At the moment this project does not implement rate-limiting. It's on the Roadmap. Please be cautious with larger chained requests.

Node.js Examples

This folder has a Node.js example that utilizes the Axios library for sending REST API messages to the Better Call Dev

This script includes functions that let you:

  • Get metadata on OBJKTs
  • Get user collections
  • Get user creations (limited)
  • Get total number of OBJKTs held by a user

1) Get User Metadata from a Tezos Address - Get Account Info - BCD API Docs

Returns Total Number of OBJKTs collected

API Endpoint: https://api.better-call.dev/v1/account/mainnet/{address}

Example:

async function getUserInfo(user) {
    try {
        const res = await axios.get('https://api.better-call.dev/v1/account/mainnet/' + user + '/token_balances')
        return res.data.total
    } catch (error) {
        return null
    }
}

2) Get OBJKT Metadata from a Tezos Address - Get Token Metadata- BCD API Docs

Returns OBJKT metadata

API Endpoint: https://api.better-call.dev/v1/tokens/mainnet/metadata?token_id=

Example:

async function getTokenInfo(id){
    try {
        const res = await axios.get('https://api.better-call.dev/v1/tokens/mainnet/metadata?token_id=' + id.toString())
        return res.data[0]
    } catch (error) {
        return null
    }
}

3) Get OBJKTs created from a Tezos Address - Get Token Metadata- BCD API Docs

Returns OBJKTs created from passed Creator Tezos Address. Paginated API response. Function includes input to offset results.

API Endpoint: https://api.better-call.dev/v1/tokens/mainnet/metadata?creator=

Example:

async function getCreations(id,offset){
  try {
      const res = await axios.get('https://api.better-call.dev/v1/tokens/mainnet/metadata?creator=' + id + '&size=10&offset='+offset)
      return res.data
  } catch (error) {
      return null
  }
}

4) Get OBJKTs collected from a Tezos Address - Get Account Balances- BCD API Docs

Returns OBJKTs collected by Tezos Address. Paginated API response. Function includes input to offset results.

API Endpoint: https://api.better-call.dev/v1/account/mainnet/{address}//token_balances

Example:

async function getCollection(user,offset) { 
  try {
      const res = await axios.get('https://api.better-call.dev/v1/account/mainnet/' + user + '/token_balances?size=10&offset=' + offset)
      return res.data
  } catch (error) {
       return null
  }
}

5) Using Promise.All to get Metadata from multiple OBJKTs

Example:

async function getObjkts(id){
  let queue = new Array()
  if(typeof id == 'object'){
    id.forEach(element => {
      queue.push(getTokenInfo(element))
    });
    Promise.all(queue).then((values) => {
      console.log(values)
    });
  }else{
    queue.push(getTokenInfo(id))
    Promise.all(queue).then((values) => {
      console.log(values)
    });
  }
}

getObjkts([36899,79321,78993])

6) Filtering Balances for GLTF Models

Example:

let user = 'tz1dy6DgvAjeBZpfRE3NoL84BRm4tupyKfFf'
getUserInfo(user).then((response) => {
  let collection = new Array()
  for (let index = 0; index < response / 10; index++) {
    collection.push(getCollection(user,index*10))
  }
  Promise.all(collection).then((values) => {
    let balances = new Array()
    values.flat().forEach(element => {
      balances = balances.concat(element.balances)
    });  
    let models = balances.filter(item => item.formats)
    models = models.filter(item => item.formats[0].mimeType.includes('gltf'))
  });
});