# How do I connect my Ethereum Node using the Web3 interface?

The web3.js library makes it simple to connect and interact with Ethereum nodes.

This knowledgebase article contains sample JS Code that should help explain how to use the web3.js library to connect to your Bloq Ethereum node.

If you haven't done so already, please see the [Bloq Node Setup](/advanced-documentation/developers-guide/bloqnode-setup.md) page to spin up your Ethereum node.

### Requirements <a href="#requirements" id="requirements"></a>

First, ensure the Web3 libraries are installed by following this [guide](https://web3js.readthedocs.io/en/v1.2.1/getting-started.html).

Once the libraries have been installed, you can connect to your Ethereum node with the sample code. Some of the key steps within the sample code includes:

1. Setting up parameters
2. Creating a Web3 http provider with credentials
3. Disregarding self-signed certificates
4. Creating the Web3 instance
5. Using the Web3 interface to fetch the current block number

### 1. Setting up Parameters <a href="#id-1-setting-up-parameters" id="id-1-setting-up-parameters"></a>

```javascript
'use strict'

const https = require('https')
const Web3 = require('web3')

const nodeUrl = 'https://<BLOQ_ETH_NODE_IP_ADDRESS>:8545'
```

### 2. Creating a Web3 http provider with credentials <a href="#id-2-creating-a-web3-http-provider-with-credentials" id="id-2-creating-a-web3-http-provider-with-credentials"></a>

#### Option 1: Basic Authentication <a href="#option-1-basic-authentication" id="option-1-basic-authentication"></a>

```javascript
const user = 'USER'
const password = 'PASSWORD'

// Create a Web3 HTTP provider with the basic auth credentials set
const web3Provider = new Web3.providers.HttpProvider(nodeUrl, {
  headers: [
    {
      name: 'Authorization',
      value: `Basic ${Buffer.from(`${user}:${password}`).toString('base64')}`
    }
  ]
})
```

#### Option 2: Using JWT <a href="#option-2-using-jwt" id="option-2-using-jwt"></a>

```javascript
const token = 'TOKEN'

// Create a Web3 HTTP provider with JWT set
const web3Provider = new Web3.providers.HttpProvider(nodeUrl, {
  headers: [
    {
      name: 'Authorization',
      value: `Bearer ${token}`
    }
  ]
})
```

### 3. Creating a Web3 websocket provider with credentials <a href="#id-3-creating-a-web3-websocket-provider-with-credentials" id="id-3-creating-a-web3-websocket-provider-with-credentials"></a>

#### Option 1: Basic Authentication <a href="#option-1-basic-authentication-2" id="option-1-basic-authentication-2"></a>

```javascript
const user = 'USER'
const password = 'PASSWORD'

// Create a Web3 WS provider with the basic auth credentials set
const web3Provider = new Web3.providers.WebsocketProvider(nodeUrl, {
  headers: {
    authorization: `Basic ${Buffer.from(`${user}:${password}`).toString(
      'base64'
    )}`
  }
})
```

#### Option 2: Using JWT <a href="#option-2-using-jwt-2" id="option-2-using-jwt-2"></a>

```javascript
const token = 'TOKEN'

// Create a Web3 WS provider with JWT set
const web3Provider = new Web3.providers.HttpProvider(nodeUrl, {
  headers: { authorization: `Bearer ${token}` }
})
```

### 4. Disregarding Self-Signed Certificates <a href="#id-4-disregarding-self-signed-certificates" id="id-4-disregarding-self-signed-certificates"></a>

```javascript
// Create a Web3 WS provider with basic auth credentials
// that disregards self-signed certificates
const web3Provider = new Web3.providers.WebsocketProvider(nodeUrl, {
  requestOptions: { rejectUnauthorized: false },
  headers: {
    authorization: `Basic ${Buffer.from(`${user}:${password}`).toString(
      'base64'
    )}`
  }
})

// Set the HTTPS agent in that provider to avoid complaining about
// self-signed certificates
web3Provider.httpsAgent = new https.Agent({ rejectUnauthorized: false })
```

### Additional Resources <a href="#additional-resources" id="additional-resources"></a>

Web3 Documentation: <https://web3js.readthedocs.io/en/v1.2.0/web3-eth.html>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.bloq.com/advanced-documentation/knowledgeable/how-do-i-connect-my-ethereum-node-using-the-web3-interface.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
