npm package: @moneylion/engine-api
Description: Interface to engine.tech API
Latest version: 1.3.8

# Engine API

This is a Javascript interface to the [Engine](https://engine.tech/docs/api-reference/) API service.

## Getting Started

Install the library

```
npm add @moneylion/engine-api
```

Request an authentication token and API endpoint from Engine.

### Creating a Client Instance

Normally you won't have to create a client instance, but you can do so if you want to make a request that this library doesn't support.

```
import { Client } from "@moneylion/engine-api";

new Client(endpoint, authentication_token);
```

This provides `get`, `post`, and `patch` methods that accept a REST endpoint and a request body.
The request body will be serialized to JSON before being sent.
If you want to pass a body as a string or use a different HTTP method, use the `request` method.

```
client.request("/endpoint", "DELETE", "body as a string");
```

### Creating a Lead

To create a lead, you can do the following:

```
import { Lead } from "@moneylion/engine-api";

new Lead(endpoint, authentication_token).create(lead);
```

This will return a promise that resolves with the UUID of the lead that was just created.
It will not create a rate table yet.
In order to create a rate table you can do the following:

```
import { Lead } from "@moneylion/engine-api";

new Lead(endpoint, authentication_token).getRateTable(lead);
```

This will return a promise that resolves with an AsyncRateTable.
You can use the `AsyncRateTable.resolve()` method to turn it into a real RateTable.
You can also use `getRateTableBlocking` which will resolve with a RateTable, but you will potentially spend longer waiting for network requests.

You can do the lead creation and rate table fetch in one step using the `createAndGetRateTable` and `createAndGetRateTableBlocking` methods.

### Updating a Lead

To update a lead, you can do the following:

```
import { Lead } from "@moneylion/engine-api";

new Lead(endpoint, authentication_token).update(leadUuid, updatedLead);
```

This returns a promise but will _not_ resolve with a rate table and will instead resolve the lead UUID back to you when it finishes.

### Rate Tables

Rate tables are returned when you create a lead.
If you would like to retrieve a rate table manually you can request it by its UUID.

```
import { AsyncRateTable } from "@moneylion/engine-api";

new AsyncRateTable({ uuid: rateTableUuid, host: endpoint, api_token: authentication_token }).resolve();

// You can also pass a pre-created client instead of endpoint and token
new AsyncRateTable({ uuid: rateTableUuid, client: client }).resolve();

// You can also pass a RateTable object instead of a uuid.
new AsyncRateTable({ rateTable: rateTable, client: client }).resolve();
```

### Offer Catalog Rate Tables

Offer catalog rate tables work similarly to regular rate tables but use different endpoints and require `offerCatalogProductTypes` instead of `productTypes`.

#### Creating an Offer Catalog Rate Table

```
import { Leads } from "@moneylion/engine-api";

const leads = new Leads(endpoint, authentication_token);

// The lead data should include offerCatalogProductTypes
const leadData = {
  personalInformation: {
    firstName: "John",
    lastName: "Doe",
    email: "john@example.com",
    // ... other fields
  },
  financialInformation: {
    annualIncome: 75000,
    // ... other fields
  },
  loanInformation: {
    purpose: "debt_consolidation",
    loanAmount: 10000
  },
  productTypes: ["DebtSettlement"]  // Product types for offer catalog
};

// First, create or get a lead UUID
const leadUuid = await leads.create(leadData);

// Non-blocking: returns AsyncOfferCatalogRateTable that you can resolve
const asyncRateTable = await leads.getOfferCatalogRateTable(leadUuid, leadData);
const rateTable = await asyncRateTable.resolve();

// Blocking: waits for the final rate table
const rateTable = await leads.getOfferCatalogRateTableBlocking(leadUuid, leadData);
```

#### Fetching an Existing Offer Catalog Rate Table

If you already have a rate table UUID, you can fetch it directly:

```
import { AsyncOfferCatalogRateTable } from "@moneylion/engine-api";

new AsyncOfferCatalogRateTable({ uuid: offerCatalogRateTableUuid, host: endpoint, auth_token: authentication_token }).resolve();

// You can also pass a pre-created client instead of endpoint and token
new AsyncOfferCatalogRateTable({ uuid: offerCatalogRateTableUuid, client: client }).resolve();

// You can also pass an OfferCatalogRateTable object instead of a uuid.
new AsyncOfferCatalogRateTable({ rateTable: offerCatalogRateTable, client: client }).resolve();
```

The `AsyncOfferCatalogRateTable` class handles polling automatically, just like `AsyncRateTable`, waiting for all pending responses to complete before resolving.

## For Contributors

This is a relatively normal NPM and Typescript project.

1. `npm install` to install dependencies
2. `npm run build` to build the project
3. `npm test` to run the tests

# Publishing to NPM

Run the following steps to publish the library to NPM.

0. Update the version number in `package.json`. Commit this change.
1. `npm run build` - This will run Typescript to compile the Javascript.
2. `npm publish --access=public`. This will publish the library.