# Setting Prices for Charging

Learn how to set prices for EV charging.

Rates allow you to set a price for charging sessions on a connector. By setting a Rate, eDRV will automatically calculate and report costs for an ongoing session via Webhook events in the `session.cost` property so your application does not have to.

You can currently set rates on a connector as a combination of any of the following elements:

* ENERGY : energy delivered
* TIME : energy consumption period
* FLAT : a flat fee
* PARKING TIME : parking time fee with grace period\
  See [Rates](https://docs.edrv.io/docs/prices-rates) for further details.

Rates are also used for eDRV's [Charging Intelligence](https://docs.edrv.io/docs/charging-intelligence) for e.g. if you want to set a cap on costs for a charging session.

Please see the Coming Soon section for more Complex Rates such as:

* Time of Day Rates: For example daytime vs. night time rates.
* Overstay Rates: To encourage drivers to vacate chargestations as soon as possible after a full charge.
* Rates by User Groups: Give VIPs and loyal customers special rates.

## Prerequisites

1. Have an API token and verified it is working for you. See [Token Request](https://docs.edrv.io/docs/token-request)
2. Get Connector Id. Rates are attached to a connector
   > You can get this by calling the `GET /chargestations` endpoint.

## Step 1: Create a Rate

```javascript create_rate.js
const request = require('request');

const options = {
  method: 'POST',
  url: 'https://api.edrv.io/v1.1/rates',
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json',
    Authorization: 'Bearer API_KEY'
  },
  body: {
    price_components: [
      {type: 'FLAT', price: 10.55, tax: 16}, 
      {type: 'ENERGY', price: 0.5, tax: 16}],
    meta_data: {mydata: 'value', mydata2: 'value2'}
  },
  json: true
};

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
  /*Save rate Id within your appication*/
  const rate_id = body.result._id;
  
});
```

## Step 2: Attach Rate to a Connector

```javascript attach_rate.js
const connectorId = "5fca57b3fbfcc320b3b03f22";

const options = {
  method: 'PATCH',
  url: 'https://api.edrv.io/v1.1/connectors/${connectorId}',
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json',
    Authorization: 'Bearer API_KEY'
  },
  body: {rate: rate_id},
  json: true
};

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});
```

## Step 3: Start a new Session

When a new charging session is started on this connector, the attached `connector.rate` is used to calculate instantaneous `session.cost` and is reported with every new chargestation energy report to your applicaiton server via [Webhooks](https://docs.edrv.io/docs/webhooks-tutorial)

See [Start Charging](https://docs.edrv.io/docs/start-charging) on how to start a new charging session.

## Complex Rates (Coming Soon)

### Time of Day Rates: Available in `Private Beta` for `Enterprise` customers

if you would like to set different prices based on the time of day e.g $0.25/kWh 6Pm - 8Am(night) vs $0.40/kWh 8Am - 6Pm (day).

### Rates by User Groups

Give special rates and connector access to groups of users. E.g. offer special rates to drivers who have a subscription within your app. Or offer VIP drivers access to certain chargestations at a special rate.

eDRV takes a host of signals from the chargestation to accurately measure and report accurate session costs to your application servers even with complex and varying rates.