> For the complete documentation index, see [llms.txt](https://samypesse.gitbook.io/commercetools-demo/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://samypesse.gitbook.io/commercetools-demo/cli/csv-parser-price.md).

# CSV Price Parser

[![Travis Build Status](https://img.shields.io/travis/commercetools/csv-parser-price/master.svg?style=flat-square)](https://travis-ci.org/commercetools/csv-parser-price) [![Codecov Coverage Status](https://img.shields.io/codecov/c/github/commercetools/csv-parser-price.svg?style=flat-square)](https://codecov.io/gh/commercetools/csv-parser-price) [![David Dependencies Status](https://img.shields.io/david/commercetools/csv-parser-price.svg?style=flat-square)](https://david-dm.org/commercetools/csv-parser-price) [![David devDependencies Status](https://img.shields.io/david/dev/commercetools/csv-parser-price.svg?style=flat-square)](https://david-dm.org/commercetools/csv-parser-price?type=dev)

Convert [commercetools price](https://docs.commercetools.com/http-api-projects-products.html#price) CSV data to JSON. See example below for CSV format and sample response

## Usage

`npm install @commercetools/csv-parser-price --global`

### CLI

```
Usage: csvparserprice [options]
Convert commercetools price CSV data to JSON.

Options:
  --help, -h        Show help text.                                    [boolean]
  --version, -v     Show version number.                               [boolean]
  --inputFile, -i   Path to input CSV file.                   [default: "stdin"]
  --outputFile, -o  Path to output JSON file.                [default: "stdout"]
  --apiUrl          The host URL of the HTTP API service.
                                              [default: "https://api.europe-west1.gcp.commercetools.com"]
  --authUrl         The host URL of the OAuth API service.
                                             [default: "https://auth.europe-west1.gcp.commercetools.com"]
  --batchSize, -b   Number of CSV rows to handle simultaneously.  [default: 100]
  --delimiter, -d   Used CSV delimiter.                           [default: ","]
  --accessToken     CTP client access token
  --projectKey, -p  API project key.                                  [required]
  --logLevel        Logging level: error, warn, info or verbose.
                                                               [default: "info"]
  --logFile         Path to file where to save logs.
                                                 [default: "csvparserprice.log"]
```

Note that when the stdout is used as an output stream all log messages are written by default to `csvparserprice.log` log file.

### JS

```js
const fs = require('fs')
const CsvParserPrice = require('@commercetools/csv-parser-price')

const csvParserPrice = new CsvParserPrice(
  {
    projectKey: process.env.CT_PROJECT_KEY,
    credentials: {
      clientId: process.env.CT_CLIENT_ID,
      clientSecret: process.env.CT_CLIENT_SECRET,
    },
    accessToken: '<tokenfromapi>',
  },
  {
    error: console.error,
    warn: console.warn,
    info: console.log,
    verbose: console.log,
  },
  {
    delimiter: '^',
  }
)

const outputStream = fs.createWriteStream('./output.json')

outputStream.on('error', (err) => {
  // handle error event
})

csvParserPrice.parse(fs.createReadStream('./input.csv'), outputStream)
```

Errors on the level `error` come from events that are fatal and thus stop the stream of data.

## Configuration

`CsvParserPrice` accepts three objects as arguments:

* API client credentials for the [authentication middleware](https://commercetools.github.io/nodejs/docs/sdk/api/createAuthMiddlewareForClientCredentialsFlow.html) (*required*)
* Logger takes object with four functions (*optional*)
* Config (*optional*)
  * `batchSize`: number of CSV rows to handle simultaneously. (*default*: `100`)
  * `delimiter`: the used CSV delimiter (*default*: `,`)

Sample CSV file

```csv
variant-sku,value.currencyCode,value.centAmount,country,customerGroup.groupName,channel.key,validFrom,validUntil,customType,customField.foo,customField.bar,customField.current,customField.name.nl,customField.name.de,customField.status,customField.price,customField.priceset
my-price,EUR,4200,DE,customer-group,my-channel,2016-11-01T08:01:19+0000,2016-12-01T08:03:10+0000,custom-type,12,nac,true,Selwyn,Merkel,Ready,EUR 1200,"1,2,3,5"
my-price2,EUR,4200,DE,customer-group,my-channel,2016-11-01T08:01:19+0000,2016-12-01T08:03:10+0000,custom-type,12,nac,true,Selwyn,Merkel,Ready,EUR 1200,"1,2,3,5"
my-price,EUR,4200,DE,customer-group,my-channel,2016-11-01T08:01:19+0000,2016-12-01T08:03:10+0000,custom-type,12,nac,true,Selwyn,Merkel,Ready,EUR 1200,"1,2,3,5"
```

JSON object returned from the conversion of the CSV file above

```json
{
  "prices": [
    {
      "sku": "my-price",
      "prices": [
        {
          "value": {
            "centAmount": 4200,
            "fractionDigits": 2,
            "type": "centPrecision"
          },
          "country": "DE",
          "customerGroup": {
            "id": "customer-group"
          },
          "channel": {
            "id": "my-channel"
          },
          "validFrom": "2016-11-01T08:01:19+0000",
          "validUntil": "2016-12-01T08:03:10+0000",
          "custom": {
            "type": {},
            "fields": {
              "foo": 12
            }
          }
        },
        {
          "value": {
            "centAmount": 4200,
            "fractionDigits": 2,
            "type": "centPrecision"
          },
          "country": "DE",
          "customerGroup": {
            "id": "customer-group"
          },
          "channel": {
            "id": "my-channel"
          },
          "validFrom": "2016-11-01T08:01:19+0000",
          "validUntil": "2016-12-01T08:03:10+0000",
          "custom": {
            "type": {},
            "fields": {
              "foo": 12
            }
          }
        }
      ]
    },
    {
      "sku": "my-price2",
      "prices": [
        {
          "value": {
            "centAmount": 4200,
            "fractionDigits": 2,
            "type": "centPrecision"
          },
          "country": "DE",
          "customerGroup": {
            "id": "customer-group"
          },
          "channel": {
            "id": "my-channel"
          },
          "validFrom": "2016-11-01T08:01:19+0000",
          "validUntil": "2016-12-01T08:03:10+0000",
          "custom": {
            "type": {},
            "fields": {
              "foo": 12
            }
          }
        }
      ]
    }
  ]
}
```
