# How to use Bitquery APIs with NodeJS

**URL:** <https://community.bitquery.io/t/how-to-use-bitquery-apis-with-nodejs/352>\
**Category:** GraphQL Tutorials\
**Tags:** nodejs\
**Created:** [July 28, 2021, 7:52am UTC](https://community.bitquery.io/t/how-to-use-bitquery-apis-with-nodejs/352 "2021-07-28T07:52:26Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![sayon](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@sayon](https://community.bitquery.io/u/sayon)\
**Post date:** [July 28, 2021, 7:52am UTC](https://community.bitquery.io/t/how-to-use-bitquery-apis-with-nodejs/352/1 "2021-07-28T07:52:26Z")

</div>

**NodeJS** is an open-source, cross-platform, back-end JavaScript runtime environment that runs on the V8 engine and executes JavaScript code outside a web browser. NodeJS or Node.js is extensively used in order to make scalable and dynamic web applications.

**ExpressJS** , is one of the most popular microservice framework for NodeJS web applications.

However, connecting Bitquery’s GraphQL API with a NodeJS web application is not a big deal. In this tutorial, we will connect a simple NodeJS web app and query the total counts of all the blocks in the Bitcoin DEX using Bitquery GraphQL API.

# Sign up to [Bitquery.io](http://bitquery.io/)

If you are new to Bitquery’s interface, go to [GraphQL IDE](https://graphql.bitquery.io/ide).

 ![](https://us1.discourse-cdn.com/flex016/uploads/bitquery/original/1X/027e112bce7c939331d257e5a3c765c69a849ff0.png)

From here, sign up with your credentials. You might get a confirmation email on your registered email address.

 ![](https://us1.discourse-cdn.com/flex016/uploads/bitquery/original/1X/a33464ae267c8b81fbb8c786268b208f38e54394.png)

After you are signed up, you would be redirected to [GraphQL IDE](https://graphql.bitquery.io/ide) from where you can get your unique API key.

 ![](https://us1.discourse-cdn.com/flex016/uploads/bitquery/original/1X/5f0b8b00ae92e551b4a5d77293b688764b847600.png)

# Initializing your NodeJS web application

Using the following command in the terminal, you can initialize a NodeJS web application considering the fact that you already have **npm** installed in your system.

```auto
npm init

```

Go through the setup process via the command line and when done, you’ll notice a **package.json** file appearing in the root directory of your project.

 ![Screenshot (188)](https://us1.discourse-cdn.com/flex016/uploads/bitquery/original/1X/9ccc9b2d972d4dec7b169c93e8764f532f0f39cb.png)

# Installing Dependencies

For our simple NodeJS web application, we have used **EJS** as the main _templating library_ in order to display dynamic content on our web pages of our application.

```auto
npm install ejs 

```

Also, in order to use the **fetch** API method in our Node.js application, to make calls to Bitquery APIs, we need to install **node-fetch** module as well.

```auto
npm install node-fetch

```

Alas, in order to use **ExpressJS** , we need to install it too via npm.

```auto
npm install express

```

# Getting Started with EJS (optional)

If you are familiar with the EJS templating library requirements, then you can skip to the next part. But if you are not then, you would need to create a folder with the name ‘ **views** ’ in the root folder of your NodeJS project.  
This is where all your templates would be kept and the file format would be **.ejs**. For example, _index.ejs_ instead of _index.html_.

# Importing Libraries & View Engine Setup

First, in the **index.js** file we’ll import all the necessary libraries and initialize the ‘EJS’ as the view engine.

```auto
const express = require('express')
const fetch = require('node-fetch')
const app = express()
app.set('view engine', 'ejs')

```

We will be using express as the main library for running servers. In the above code, we have used `app.set('view engine', 'ejs')` in order to initialize “EJS” as the view engine.

# Declaring the Bitquery API endpoint and the GraphQL query

**[https://graphql.bitquery.io/](https://graphql.bitquery.io/)** is the Bitquery API endpoint. In the **index.js** file, below the imported libraries, declare the API endpoint and the GraphQL query as shown below.

```auto
const endpoint = "https://graphql.bitquery.io/" 
const query = `
{
	bitcoin{
    	blocks{
      		count
    	}
   	}
}`

```

# Making the API call

In this tutorial, we have created a function named _BitqueryAPICall_ which uses the _fetch_ method to make the API call. The function definition is shown below.

```auto
let countValue = 0
function BitqueryAPICall(){
	const data = fetch(endpoint, {
	    method: "POST",
	    headers: {
	    	"Content-Type": "application/json",
	      	"X-API-KEY": "BQYUGuoO6tZKM20I0lfBNCTEC4ouBCT1"
	    },
	    body: JSON.stringify({
	        query
	    })
	}).then(res => res.json()).then(data => {
		countValue = data.data.bitcoin.blocks[0].count
		console.log(countValue)
	})

}

```

After that, we will save the value returned by the function in a variable called _countValue_ as the value returned is the count of the blocks in the Bitcoin DEX.

# Sending the countValue to the template and running the NodeJS server

```auto
app.get('/', (req, res)=>{
	console.log(countValue+" is the returned value!")
	res.render('home', {result: countValue})
})

const PORT = 3000 || process.env.PORT 

app.listen(PORT, ()=>{console.log(`Server starting in port ${PORT}. `)})

```

In the above code snippet, _countValue_ gets referenced under the alias _result_ in the template ( **home.ejs** ) which would load when the server runs in the port 3000.

# views/home.ejs

The below is the home template which would load when the NodeJS server starts running.

```auto
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- Bitquery Logo -->
    <link rel="icon" href="https://test.bitquery.io/wp-content/uploads/2020/08/favicon.png" />
    <link rel="apple-touch-icon" href="https://test.bitquery.io/wp-content/uploads/2020/08/favicon.png" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>NodeJS with Bitquery API</title>
    <script src="../index.js"></script> 
</head>
<body>
	<style>
		header{
			background-color: mediumslateblue; 
			color: white;
			font-family: 'Lucida Sans';
			text-align: center; 
			margin: 5px;
		}
		body {
			background: linear-gradient(to bottom right, #DBFBF3, #F5FEFC)
		}
		h2{
			text-align: center;
			margin: 10px;
			padding: 10px;
			font-family: 'Lucida Sans' ;
		}
	</style>
	<header>
		<h1>Using Bitquery GraphQL API with NodeJS</h1>
	</header>
	<h2>The count of all the blocks in the Bitcoin DEX is <%= result %></h2>
</body>
</html>

```

**Notice**

`<h2>The count of all the blocks in the Bitcoin DEX is <%= result %></h2>`

**the `<h2>` tag is formatted in such a way that the aliased variable result would show the data returned by the query.**

# Run the NodeJS application

```auto
node index

```

 ![Screenshot (189)](https://us1.discourse-cdn.com/flex016/uploads/bitquery/original/1X/71a14b9788088712382daec801975fb2d0d915e0.jpeg)

> **Github Repository**

[https://github.com/sayon-bitquery/bitquery-nodejs](https://github.com/sayon-bitquery/bitquery-nodejs)
