# RedeemableToken Service

If you only require Redeemable Token API functionality, you can import the module separately to the main SDK.

For example:

```javascript
const { RedeemableTokenService } = require('n-membership-sdk');
const tokenService = new RedeemableTokenService(config);
```

## Table of Contents

* [Config](#config)
	* [How to obtain a key](#how-to-obtain-a-key)
* [Methods](#methods)
	* [redeem](#redeem)
	* [getPersonalisedToken](#getPersonalisedToken)

## Config

* See common configuration options in the [README file](./README.doc)
* `redeemableTokenApiKey`

```javascript
const config = new ProductionConfiguration({
	redeemableTokenApiKey: 'secret',
	redeemableEmailTokenApiKey: 'secret'
});
```

### How to obtain a key

Do one of the following:

1. Check in Doppler for your app to see if a redeemable token API key already exists.
2. Contact #membership-support slack channel for a redeemable token key.

## Methods

### `redeem(token: RedeemableToken | PersonalisedToken, userId?: string)`

```javascript
const redeemedToken = tokenService.redeem(token, userId);
```

| Parameters | Required                                           | Type              | Example                              | Description                                                                                   |
| ---------- | -------------------------------------------------- | ----------------- | ------------------------------------ | --------------------------------------------------------------------------------------------- |
| token      | Yes                                                | `RedeemableToken` | `PersonalisedToken`                  | The token to redeem                                                                           |
| userId     | No (if the token is a type of `PersonalisedToken`) | String            | xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | The user's ID. This can be retrieved from [graphql.getUserDetails](GRAPHQL.md#getUserDetails) |

**Returns**:

-   In the instance where the token given was a `RedeemableToken` an new `Promise<RedeemableToken>` will be returned with the updated data
-   In the instance where the token given was a `PersonalisedToken` the token given will be returned


### getPersonalisedToken

```js
const personalisedToken = tokenService.getPersonalisedToken('d9ca829…');
```

| Parameters | Required | Type   | Example  | Description  |
| ---------- | -------- | ------ | -------- | ------------ |
| tokenId    | Yes      | String | d9ca829… | The token ID |

**Returns**:

An instance of a [`PersonalisedToken`](../models/PERSONALISED-TOKEN.md).
