➡️ Calling Async Endpoints

Providing large projects with scalable endpoints

Asynchronous endpoints are designed to provide greater scalability for projects handling thousands of simultaneous requests.

Why Asynchronous Endpoints Offer Superior Robustness

Asynchronous endpoints are designed to prevent request timeouts, which is especially important when interacting with blockchains. Response times from blockchains can range from seconds to minutes, and some transactions may take longer than usual to be confirmed or may even drop. State-changing requests on a blockchain, such as POST, PUT, and PATCH, require sending transactions with their respective gas fees and variable wait times.

The Co:Create platform manages this complexity for you. We ensure that transactions are re-sent as needed, guaranteeing a response confirming that your transaction has been written to the blockchain.

In practice, this means you can mint thousands of tokens and receive confirmations even if the transaction takes a long time to be confirmed. You can also build a robust front end without worrying about component timeouts.

How to Use Asynchronous Endpoints?

To interact with our asynchronous endpoints, send a POST request and then poll the returned value using GET requests to another endpoint.

For example, to deploy an ERC-721 token, send a POST request to <https://api.testcocrt.xyz/alpha/erc721/async>, and store the returned value. This value will be your deployment request UUID:

{
  "data": {
    "status_id": "87115ab1-9629-4fa3-bfc8-0603372e4af0"
  }
}

After sending the request, retrieve the status of your call using a GET request that returns a status property with values such as active, delayed, or waiting. Continue polling until the request is completed, at which point the status will be completed, and the values of the completed request will be returned.

Here's a code example that demonstrates deploying an ERC-721 contract and polling the deployment status until it reaches completed:

import fetch from 'node-fetch';

let url = 'https://api.testcocrt.xyz/alpha/erc721/async';
const token = 'Bearer <<API_KEY>>';
let postDataDeploy = {
  name: 'My Token',
  symbol: 'SBT',
  base_uri: 'https://nftstorage.link/ipfs/bafybeiasv4llnkervuprzfte5sulodkzobcmpfc7ldwybdwiqq5ywbbstu/',
};
const optionsDeploy = {
  method: 'POST',
  withCredentials: true,
  credentials: 'include',
  body: JSON.stringify(postDataDeploy),
  headers: {
    'Content-Type': 'application/json',
    Authorization: `${token}`,
  },
};
fetch(url, optionsDeploy)
  .then((res) => res.json())
  .then((data) => {
    data = data.data;
    // Get the uuid returned by the deployment request
    let uuid = data.status_id;
    // Concatenate the uuid to the URL
    url = `https://api.testcocrt.xyz/alpha/erc721/async/status/${uuid}`;
    const optionsDeployGetStatus = {
      method: 'GET',
      withCredentials: true,
      credentials: 'include',
      headers: {
        'Content-Type': 'application/json',
        Authorization: `${token}`,
      },
    };
    // Check the status of the deployment every 2 seconds
    let intervalId = setInterval(fetchStatus, 2000);
    function fetchStatus() {
      fetch(url, optionsDeployGetStatus)
        .then((res) => res.json())
        .then((data) => {
          data = data.data;
          return data;
        })
        .then((data) => {
          if (data.status === 'active' || data.status === 'delayed' || data.status === 'waiting') {
            // Get and show the message
            console.log(`Deployment status: ${data.status}`);
          } else if (data.status == 'completed') {
            // Get and show the message
            console.log(`Deployment status: ${data.status}, deployed contract address: ${data.value.contract_address}`);
           // Stop polling for status
            clearInterval(intervalId);
          }
        });
    }
  })
  .catch((error) => console.error(error));

{
  status: 'completed',
  value: {
    contract_address: '0x3500C2B1795Ac7C16c1c35eA8FCD83C01DCcb0Fe',
    erc721_id: 'b6cce1bb-a44f-4ecc-aabc-dc68d60ca288'
  }
}

Check out these example code recipes to explore async requests further:

All contracts supported by Co:Create now have an asynchronous endpoint. You can find and try all async endpoints in the API Reference.

👍

Get Help

If you get stuck at any time, reach out to us on Discord or contact us via email at [email protected].