# 4. Connect your service to an OCN Node

If you have followed the previous steps in this tutorial you should now have the following things ready for making your connection to the Open Charging Network:

* [A backend service that has an OCN-ready API and Signing service](/legacy-documentation/solutions-2023/open-charging-network/connect-an-ocpi-ocn-party-to-a-node/1.-make-your-backend-service-ocn-ready.md)
* [An OCN Identity](/legacy-documentation/solutions-2023/open-charging-network/create-and-manage-an-ocn-identity.md)
* [The OCN Identity of your selected OCN Node, and a registration token](/legacy-documentation/solutions-2023/open-charging-network/connect-an-ocpi-ocn-party-to-a-node/2.-select-an-ocn-node-and-register-in-ocn-registry.md) (TOKEN\_A in OCPI Terms)

#### Follow the steps listed below to connect to your OCN Node <a href="#steps-for-making-the-connection-to-your-ocn-node" id="steps-for-making-the-connection-to-your-ocn-node"></a>

1. [Get the Public URL of your selected OCN Node](#1.-get-the-public-url-of-your-selected-ocn-node)
2. [Get OCN Node versions and endpoints](#2.-get-ocn-node-versions-and-endpoints)
3. [Ensure OCN Node can access our versions and endpoints](#3.-ensure-ocn-node-can-access-our-versions-and-endpoints)
4. [Credentials handshake](#4.-credentials-handshake)

{% hint style="info" %}
Access the full OCN technical documentation [here](https://shareandcharge.com/wp-content/uploads/2020/11/OCN-Documentation-v1.1.pdf).
{% endhint %}

## 1. Get the Public URL of your selected OCN node from the OCN Registry

There are three methods of interacting with the OCN Registry:&#x20;

1. [CLI](https://github.com/energywebfoundation/ocn-registry-elia#cli)
2. [Typescript Library](https://github.com/energywebfoundation/ocn-registry-elia#typescript-library)
3. [Java Library](https://github.com/energywebfoundation/ocn-registry-elia#java-library)

### Using the CLI

If you are using the [Command Line Interface provided in the OCN Registry Repository](https://github.com/energywebfoundation/ocn-registry#cli) on GitHub, you can check the Public URL of your selected OCN Node by doing this:

`ocn-registry get-node 0xEada1b2521115e07578DBD9595B52359E9900104`

Where `0xEada1b2521115e07578DBD9595B52359E9900104` is the operator's public address.

### Using the Typescript Library

You can find documentation on how to install and use the library [here](https://github.com/energywebfoundation/ocn-registry#typescript-library).&#x20;

The Public URL could look something like this: `https://test-node.emobilify.com`

Please visit the [OCN Registry Repository](https://bitbucket.org/shareandcharge/ocn-registry) for further details about this step.

Once connected to the registry, you can use the getNode method to return the operator's public URL:&#x20;

```
     /**
     * Get a registry node listing for a given operator.
     * @param operator Ethereum address of the operator.
     * @returns the domain name/url if listing exists.
     */
    public async getNode(operator: string): Promise<string | undefined> {
        this.verifyAddress(operator)
        const node = await this.registry.getNode(operator)
        return node || undefined
    }
```

[source](https://github.com/energywebfoundation/ocn-registry/blob/f2791a137ff2ba1fcf898bca09bb41cb2d8aa253/src/registry.ts#L64)

### Using the Java Library

Use the documentation provided [here](https://github.com/energywebfoundation/ocn-registry#java-library) to connect to the OCN Registry using the Java library.&#x20;

## 2. Get OCN Node versions and endpoints <a href="#id-2.-get-ocn-node-versions-and-endpoints" id="id-2.-get-ocn-node-versions-and-endpoints"></a>

We will first send a GET request to the OCN Node’s versions endpoint, using the TOKEN\_A we received from the faucet. Note that curl requests can be imported into e.g. Postman or Insomnia if desired.

```
curl https://test-node.emobilify.com/ocpi/versions -H 'Authorization: Token {{TOKEN_A}}'
```

You should see a JSON response with OCPI status code 1000. The response data will tell you that version 2.2 can be found at <https://test-node.emobilify.com/ocpi/2.2>.&#x20;

Do the same for this endpoint:

```
curl https://test-node.emobilify.com/ocpi/2.2 -H 'Authorization: Token {{TOKEN_A}}'
```

You should see a long list of OCPI version 2.2 endpoints implemented by the OCN Node. You will want to save these endpoints for future reference in your application’s database.&#x20;

**For now though, we just need one endpoint.** Find the endpoint URL for the module identifier “credentials”. This will be the one we use to connect to the node:

```
{
  "identifier": "credentials",
  "role": "SENDER",
  "url": "https://test-node.emobilify.com/ocpi/2.2/credentials"
}
```

## 3. Ensure OCN Node can access our versions and endpoints <a href="#id-3.-ensure-ocn-node-can-access-our-versions-and-endpoints" id="id-3.-ensure-ocn-node-can-access-our-versions-and-endpoints"></a>

During the credentials handshake, the OCN Node will attempt to request our own versions and endpoints, just as we did before in [Step 2.](#2.-get-ocn-node-versions-and-endpoints)&#x20;

The OCN Node needs to know where to contact us in case there is a message from another party that needs to be routed to us. It can also help to filter requests, in the case that we have not implemented a specific OCPI module that another party is requesting from us.

If you have not already done so, we can spin up a quick server and execite a node script that will display our versions and endpoints as follows (note: requires [NodeJS](https://nodejs.org/en/), [Express](https://expressjs.com/) and [UUID](https://www.google.com/search?q=npm+uuid\&rlz=1C5CHFA_enUS951US951\&oq=npm+uuid\&aqs=chrome.0.69i59j0i512l9.909j0j4\&sourceid=chrome\&ie=UTF-8)):

```
const express = require("express")
const uuid = require("uuid")

const app = express()

const PUBLIC_URL = "https://service.msp.com"
const TOKEN_B = uuid.v4()
console.log("Auth TOKEN_B = " + TOKEN_B)


const authorize = (req, res, next) => {
	if (req.headers.authorization !== `Token ${TOKEN_B}`) {
		return res.status(401).send({
			status_code: 2001,
			status_message: "Unauthorized",
			timestamp: new Date()
		})
	}
	next()
} 


app.get("/ocpi/versions", authorize, async (_, res) => {
	res.send({
		status_code: 1000,
		data: {
			versions: [{
				version: "2.2",
				url: `${PUBLIC_URL}/ocpi/2.2`
			}]
		},
		timestamp: new Date()
	})
})


app.get("/ocpi/2.2", authorize, async (_, res) => {
	res.send({
		status_code: 1000,
		data: {
			version: "2.2",
			endpoints: [
				/* {
					identifier: "locations",
					role: "RECEIVER",
					url: `${PUBLIC_URL}/ocpi/2.2/receiver/locations`
				} */
			]
		},
		timestamp: new Date()
	})
})


app.listen(3000, () => { console.log("Started on port 3000") })
```

This will create an authorization token that the OCN Node should use to access our own endpoints. **It is logged on start. Be sure to make a note of the authorization token as we will need it later.**&#x20;

Note also the `PUBLIC_IP`. We want the OCN Node to be able to access these endpoints from the outside, so we should make sure that the server is reachable via the public IP we set.

## 4. Credentials handshake <a href="#id-4.-credentials-handshake" id="id-4.-credentials-handshake"></a>

Now we are ready to connect to the OCN Node.&#x20;

The credentials request is detailed below. Make sure to replace the variables `TOKEN_A`, `TOKEN_B` and `PUBLIC_IP`, `PARTY_ID` and `COUNTRY_CODE` where described:&#x20;

* `TOKEN_A` should match the one generated for you by the faucet
* `TOKEN_B` should match the authorization token that you have created
* `PUBLIC_IP` should point to your publicly accessible web service
* `PARTY_ID` and `COUNTRY_CODE` refer to the same OCPI credentials that you used [when generating the registration token and entering details to the OCN Registry](/legacy-documentation/solutions-2023/open-charging-network/connect-an-ocpi-ocn-party-to-a-node/2.-select-an-ocn-node-and-register-in-ocn-registry.md)

```
curl https://test-node.emobilify.com/ocpi/2.2/credentials \
  -H 'Authorization: Token {{TOKEN_A}}' \
  -H 'Content-Type: application/json' \
  -d '{
    "token": "{{TOKEN_B}}",
    "url": "{{PUBLIC_IP}}/ocpi/versions",
    "roles": [{
      "party_id": "{{PARTY_ID}",
      "country_code": "{{COUNTRY_CODE}}",
      "role": "EMSP",
      "business_details": {
        "name": "Tutorial MSP"
      }
    }]
  }'
```

If the request is successful, you should see a response with OCPI status code 1000 and the OCN Node’s credentials returned in the body.

Now that we have connected to the Open Charging Network we can make use of OCPI to find point of interest data, issue remote commands at charge points and authorize RFID cards of any other OCPI party that is connected to the network.


---

# 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.energyweb.org/legacy-documentation/solutions-2023/open-charging-network/connect-an-ocpi-ocn-party-to-a-node/4.-connect-your-service-to-an-ocn-node.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.
