Hello, World!

Using the Blockset API is so easy- it's like a "blockchain cheat sheet". The complexity of running and maintaining a node, and learning obscure JSON-RPC commands has been reduced to simply making an HTTP request.

To show you how easy this is, let's build an API that provides some wrapper functionality around the Blockset API.

This API will expose a single endpoint (GET /the-pizza-tx), which acts as a proxy to the Blockset API, and returns back the full Bitcoin transaction of the infamous Pizza Transaction.

You can view the code for this project on Github

The Setup

We'll build a NodeJS API using express, because this guide is all about keeping it easy. We'll use dotenv to inject our Blockset API User JWT into the API, and request to make it easy to call the Blockset API from within our own API.

Start by creating a new Node project

$ mkdir hello-world
$ cd hello-world
$ npm init

Next, install the dependencies

$ npm install express dotenv request

Finally, create a new file called app.js and open it up in your favorite text editor.

$ touch app.js

The API Skeleton Code

First, we'll require our dependencies into app.js, and do some more setup

require('dotenv').config()

const express = require('express')
const request = require('request')
const app = express()
const port = 3000

// can't talk to Blockset API without a JWT!
if (!process.env.BLOCKSET_API_USER_JWT) {
  throw new Error('create a .env file and set the BLOCKSET_API_USER_JWT environment variable')
}

Now let's use express to create a new endpoint, and then start the API listener

app.get('/the-pizza-tx', (res) => {
  // we'll fill this in next...
})

app.listen(port, () => {
  console.log(`Blockset API Hello World listening at http://localhost:${port}`)
  console.log(`Navigate to http://localhost:${port}/the-pizza-tx to see the`)
  console.log('  infamous Bitcoin Pizza Transaction (https://bitcoinpizzaindex.net/)')
  console.log('πŸš€ Powered by Blockset API! πŸš€')
})

The above block of code is the entirety of our little API. Now we just need to fill in /the-pizza-tx endpoint request/response, a job that we'll farm out to the Blockset API.

The Blockset API Integration

When our GET /the-pizza-tx is called, we'll make a request to the Blockset API, get back the blockchain response data, and finally return that response data back to our API's user.

Let's start by building the Blockset API request URL. The following code pieces together the different parts of the Blockset API request URL, and creates an authorization header which is required by Blockset API to identify our request.

app.get('/the-pizza-tx', (res) => {
  
  // build the Blockset API URL
  const baseUrl = 'https://api.blockset.com'
  const transactionsPath = 'transactions'
  const transaction = 'bitcoin-mainnet:a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d'
  const url = `${baseUrl}/${transactionsPath}/${transaction}`

  // set the JWT bearer token, and specify JSON data type
  const options = {
    json: true,
    headers: {
      authorization: `Bearer ${process.env.BLOCKSET_API_USER_JWT}`
    }
  }

  // next, we'll make the request...
})

Now we can make the request to Blockset API!

app.get('/the-pizza-tx', (res) => {
  // we already set up the request data (`url` and `options`) above...

  request(url, options, (error, blocksetResponse, blocksetBody) => {
    
    // if the network request failed, respond with an error
    if (error) {
      return res.status(500).json(error)
    }

    // if the Blockset API request resulted in a non-200, respond with the error
    if (!blocksetResponse.ok) {
      return res.status(blocksetResponse.statusCode).json(blocksetBody)
    }

    // respond with the Blockset API response!
    res.json(blocksetBody)
  })
})

And... that's it!

If you run this API locally (via node app.js), and navigate to http://localhost:3000/the-pizza-tx in your browser, you'll see the full Bitcoin transaction, and associated Blockset API metadata about that transaction, which was the result of one of the first bitcoin-for-goods transfers, back on June 22, 2010.

Need help? Contact us
Blockset both powers, and is brought to you by BRD , the world’s oldest and most trusted mobile wallet.