npm package: @recast-sdk/recast-node
Description: The `recast-node` library provides the a SDK for RecastPay for [NodeJs](https://nodejs.org) applications. The SDK allows for simple management of authorisation to the RecastPay Api via the `RecastClient`, which exposes helper functions for interacting wit
Latest version: 1.2.1

# @recast-sdk/recast-node

The `recast-node` library provides the a SDK for RecastPay for
[NodeJs](https://nodejs.org) applications. The SDK allows for simple management
of authorisation to the RecastPay Api via the `RecastClient`, which exposes
helper functions for interacting with RecastPay API calls.

## `RecastClient`

`RecastClient` handles authentication token management as well as JWT
verification for the various RecastPay JWT types.

### Initialisation

To initialise the `RecastClient` we need to provide the a `RecastConfig` object
to the client, this configuration hold the API `key`, `secret` and if you are
enabling `sandbox` mode for the application.

The `key` and `secret` must not exposed to the public and are obtained from the
API jey section in [Recast for Business](https://business.recast.tv).

For a [NestJs](https://nestjs.com/) application, the initialisation may look
something like this:

```ts
import { RecastClient } from '@recast-sdk/recast-node';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class RecastService {
  readonly #RecastClient: RecastClient;

  constructor(private readonly configService: ConfigService) {
    this.#RecastClient = new RecastClient({
      key: this.configService.get<string>('RECAST_API_KEY') ?? '',
      secret: this.configService.get<string>('RECAST_API_SECRET') ?? '',
      sandbox: true,
    });
  }
}
```

### Inventory Items

To interact with inventory items, we can call `inventoryItem` to retrieve the
client functionality. `inventoryItem` is mainly intended for retrieving items
and lists of items from the RecastPay API, it can however do simple create and
update operations and can be incorporated into a Brand API inventory creation /
update process so that changes made are reflected on Recast for Business.

<!-- Add documentation on delete / de-listing functionally -->

```ts
// Depending on you API framework, retrieval of the RecastClient may change.
const recastClient = new RecastClient({
  key: 'yourRecastApiKey',
  secret: 'yourRecastApiSecret',
  sandbox: true,
});

// to get an Item use the Brands item id provided to the RecastPay API with
// the `getById` function
const item = await recastClient.inventoryItem.getById(id, brandId);

// to get a list of items, limited to the first 50 call `getList` function
const items = await recastClient.inventoryItem.getList(brandId);

// to get a list of paginated items, provide the number of items and offset to
// `getList` function
const items = await recastClient.inventoryItem.getList(brandId, { limit: 10; offset: 0 });

// Once you have the metadata provided from the inventory, you can map this with
// data from your database to present the item information as required.
```

### Purchase Intent

To make a purchase via RecastPay, the Brand API needs to generate a Purchase
Intent Token, this token is generated for a Brand product and user for a
provided Cast Credit value. The generated token can be passed to a front-end
client so a secure, verifiable purchase can be made.

```ts
// Depending on you API framework, retrieval of the RecastClient may change.
const recastClient = new RecastClient({
  key: 'yourRecastApiKey',
  secret: 'yourRecastApiSecret',
  sandbox: true,
});

// To create a new Purchase Intent, provide the Brands item id, the Brands User
// Id and how many Cast Credits to be changed.
const intent = recastClient.purchaseIntent.create(
  { externalProductId: 'ID-1234', externalUserId: 'user-1234', amount: 120 },
  brandId
);

// The intent can be returned to the client to initiate a secure purchase
```

### Product

`product` can be called to quickly check if a brand user has access to an item,
via the `getAccessStatus` call. This call does not return an `AccessToken`, it
does however return the `hasAccess` and `product` details, as well as `purchase`
details if `hasAccess` has returned true.

```ts
// Depending on you API framework, retrieval of the RecastClient may change.
const recastClient = new RecastClient({
  key: 'yourRecastApiKey',
  secret: 'yourRecastApiSecret',
  sandbox: true,
});
```