# Prescriptions

Like medication prescriptions at a pharmacy, users on the FibriCheck platform require an active prescription to analyse their measurements.

An active prescription links a user, a group, and a package.&#x20;

<figure><img src="https://550842036-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1VHmKXYQVUzvBkU504K0%2Fuploads%2FcrlrYD8ufEzJqJaeduZH%2Fdocs.fibricheck.com%20-%20prescriptions.png?alt=media&#x26;token=10ddc6f2-5323-4f90-ada0-ff8072e28d04" alt="prescription composition"><figcaption></figcaption></figure>

The relevant properties of a prescription are:

<table><thead><tr><th width="171">Property</th><th>Description</th></tr></thead><tbody><tr><td><code>hash</code></td><td>The <code>hash</code> is a random string and uniquely identifies the prescription</td></tr><tr><td><code>userId</code></td><td>The <strong>FibriCheck user</strong> to which the prescription is linked</td></tr><tr><td><code>groupId</code></td><td>The group to which the user should be added. This is can be one or more groups per integration partner.<br>You will receive a dedicated <code>groupId</code> for your integration from FibriCheck.</td></tr><tr><td><code>packageId</code></td><td>The chosen package determines the duration of the prescription, which is always limited in time. <br>You will receive a list of possible <code>packageId</code> values for your integration from FibriCheck.</td></tr><tr><td><code>isTemplate</code></td><td>Indicates if this prescription is a <a href="#create-a-prescription">template</a>.</td></tr></tbody></table>

Obtaining an active prescription involves three simple steps:&#x20;

<figure><img src="https://550842036-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1VHmKXYQVUzvBkU504K0%2Fuploads%2FsU2yrsEfZRkOJT1HIuRb%2Fdocs.fibricheck.com%20-%20prescriptions%20(1).png?alt=media&#x26;token=13e95836-1de3-4b3a-9cf9-2e70fd82daa5" alt=""><figcaption></figcaption></figure>

1. **Create a prescription hash:** typically done in the (partner's) backend or provided by FibriCheck.
2. **Scan the prescription hash:** links the prescription to the user. This can be done in the front or backend.
3. **Activate the prescription hash:** from this moment, measurements will be analyzed. This is also the start of the prescription duration.

{% hint style="warning" %}
A group can be configured with a number of activation credits. When configured, every activated prescription will consume a credit. When the credits run out, activating new prescriptions will not be possible anymore until new credits are added to the group.
{% endhint %}

## Create a prescription hash

There are multiple ways to obtain a prescription hash. For integration partners, typically the partner instead of the end-user pays for using FibriCheck. In this scenario there are 2 options:&#x20;

* **Use a template hash**\
  A template hash is linked to a package and a group and can be re-used by different users. Every user can use this template hash code at most once. This method is typically used in screening campaigns where users will only use FibriCheck for a single screening period.&#x20;
* **Generate prescriptions through the API**\
  As a partner, you also have the ability to create prescription hashes yourself. This gives you more flexibility in how your customers can use FibriCheck.

{% hint style="info" %}
Prescriptions can only be created by specific users with elevated rights. Typically only a few integration partner user accounts obtain those rights. \
Reach out via <integrations@fibricheck.com> to know more.
{% endhint %}

### REST API

Execute an API call with the following payload to create a new prescription:

```json
{
  "data": {
    "command": "create-prescription",
    "data": {
      "groupId": "{groupId}",
      "packageId": "{packageId}",
      "isTemplate" : <true|false>
    }
  }
}
```

In case you want to create a template hash, you can add the `isTemplate: true` property to the data object.

## Create a prescription

<mark style="color:green;">`POST`</mark> `https://api.fibricheck.com/tasks/v1/functions/integration/execute`

#### Request Body

| Name                                   | Type   | Description |
| -------------------------------------- | ------ | ----------- |
| data<mark style="color:red;">\*</mark> | object | payload     |

{% tabs %}
{% tab title="200: OK Successful creation of a prescription" %}

```json
{
  "result": {
    "prescription": {
      "id": "58074804b2148f3b28ad758b",
      "creator_id": "58074804b2148f3b28ad758b",
      "hash": "string",
      "reference": "string",
      "status": "FREE",
      "group_id": "58074804b2148f3b28ad758b",
      "user_id": "58074804b2148f3b28ad758b",
      "package_id": "58074804b2148f3b28ad758b",
      "duration": 0,
      "price": 0,
      "tags": [
        "string"
      ],
      "bundle_id": "58074804b2148f3b28ad758b"
    }
  }
}
```

{% endtab %}
{% endtabs %}

## Get the status of a prescription

You can query the status of a prescription to know if a prescription has been used. Execute an API call with the following payload to the endpoint below:&#x20;

```json
{
  "data": {
    "command": "get-prescription",
    "data": {
      "hash": "{hash}"
    }
  }
}
```

If the response has `result.prescription.status` set to `ACTIVATED`, the prescription has been activated by the respective user. In that case, `result.prescription.user_id` will contain the reference to the user that activated the prescription.

## Get the prescription status

<mark style="color:green;">`POST`</mark> `https://api.fibricheck.com/tasks/v1/functions/integration/execute`

{% tabs %}
{% tab title="200: OK Returns prescription status" %}

```json
{
    "result": {
        "prescription": {
            "id": "642544e20d43ab",
            "creator_id": "641ac2c659744976404b685d",
            "reference": "ABC123",
            "hash": "aa121212121212121359874db296b8115a32d3",
            "status": "PAID_BY_GROUP",
            "group_id": "6425767bc7801a7f69392577",
            "package_id": "5588424dreddd4",
            "is_template": false,
            "logs": [
                {
                    "old_status": "NOT_PAID",
                    "new_status": "PAID_BY_GROUP",
                    "timestamp": 1680177647943
                }
            ],
            "paid_timestamp": 1680177647943,
            "creation_timestamp": 1680177647943,
            "update_timestamp": 1680177647943
        }
    }
}
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
The integration REST API is implemented in a serverless computing environment. This causes some additional information to be present in the API endpoints.\
Only the information in the `result` property of the API response should be considered. \
If no prescription is present in the `result`, it indicates that no prescription is found.
{% endhint %}

## Administrative activation of a prescription

You can activate a prescription for other users than the authenticated users. The typical use case is when the partner backend activates prescriptions for a user.

### REST API

Activate an existing prescription by executing an API call with the following payload:

```json
{
  "data": {
    "command": "activate-prescription",
    "data": {
      "hash": "{prescriptionHash}",
      "userId": "{userId}"
    }
  }
}
```

## Activate a prescription

<mark style="color:green;">`POST`</mark> `https://api.fibricheck.com/tasks/v1/functions/integration/execute`

#### Request Body

| Name                                   | Type   | Description |
| -------------------------------------- | ------ | ----------- |
| data<mark style="color:red;">\*</mark> | String | payload     |

{% tabs %}
{% tab title="200: OK " %}

```javascript
{
  "result": {
    "prescription": {
      "id": "58074804b2148f3b28ad758b",
      "creator_id": "58074804b2148f3b28ad758b",
      "hash": "string",
      "reference": "string",
      "status": "FREE",
      "group_id": "58074804b2148f3b28ad758b",
      "user_id": "58074804b2148f3b28ad758b",
      "package_id": "58074804b2148f3b28ad758b",
      "duration": 0,
      "price": 0,
      "tags": [
        "string"
      ],
      "bundle_id": "58074804b2148f3b28ad758b"
    }
  }
}
```

{% endtab %}
{% endtabs %}

For ease of use, we can also combine both into one call with the following payload:

```json
{
  "data": {
    "command": "create-and-activate-prescription",
    "data": {
      "groupId": "{groupId}",
      "packageId": "{packageId}",
      "userId": "{userId}>"
    }
  }
}
```

## Generate & activate a prescription

<mark style="color:green;">`POST`</mark> `https://api.fibricheck.com/tasks/v1/functions/integration/execute`

#### Request Body

| Name                                   | Type   | Description |
| -------------------------------------- | ------ | ----------- |
| data<mark style="color:red;">\*</mark> | String | payload     |

{% tabs %}
{% tab title="200: OK " %}

```javascript
{
  "result": {
    "prescription": {
      "id": "58074804b2148f3b28ad758b",
      "creator_id": "58074804b2148f3b28ad758b",
      "hash": "string",
      "reference": "string",
      "status": "FREE",
      "group_id": "58074804b2148f3b28ad758b",
      "user_id": "58074804b2148f3b28ad758b",
      "package_id": "58074804b2148f3b28ad758b",
      "duration": 0,
      "price": 0,
      "tags": [
        "string"
      ],
      "bundle_id": "58074804b2148f3b28ad758b"
    }
  }
}
```

{% endtab %}
{% endtabs %}

## User activation of a prescription

If the user receives a prescription hash, they can activate the prescription themselves. In this case, the prescription is linked to the authenticated user.

### SDK

The `activatePrescription` method in the SDK scans the prescription, links it to the authenticated user, and activates it. This method takes the prescription hash as a parameter.

This function can throw 2 errors:&#x20;

* `NotPaidError`: A paying prescription has not yet been paid.
* `AlreadyActivatedError` : The prescription has already been used.

{% tabs %}
{% tab title="Flutter" %}

```dart
await _sdk.activatePrescription(hash);
```

{% endtab %}

{% tab title="React Native" %}

```typescript
await sdk.activatePrescription(hash);
```

{% endtab %}
{% endtabs %}

### REST API

Activating a prescription through the REST API is a two-step process. First, you have to scan the prescription, then you have to activate the prescription. You can only scan a prescription once.

The following API call will scan the prescription and link the prescription to the user that executes the API call.

## Scan a prescription

<mark style="color:blue;">`GET`</mark> `https://api.fibricheck.com/prescriptions/v1/{prescriptionHash}/scan`

{% tabs %}
{% tab title="200: OK Prescription scanned successfully" %}

```javascript
{
    "id": "63f632e4fa725244e20d421c",
    "creator_id": "6255af75590801000bdb1c30",
    "reference": "GBX4L5JB",
    "hash": "1f3f4cbb79be1aac595fb07a5e2ac16548ddf9d7",
    "status": "FREE",
    "group_id": "6258156365220500073f0c7d",
    "user_id": "5811c7a046e0fb000530a465",
    "duration": 604800000,
    "price": 1000,
    "package_id": "5923fbc44cedfd0005042cbb",
    "is_template": false,
    "logs": [
        {
            "old_status": "NOT_PAID",
            "new_status": "FREE",
            "timestamp": 1677079268566
        }
    ],
    "paid_timestamp": 1677079268565,
    "creation_timestamp": 1677079268566,
    "update_timestamp": 1677079364798
}
```

{% endtab %}

{% tab title="400: Bad Request When a prescription is already scanned" %}

```javascript
{
    "code": 502,
    "name": "ALREADY_SCANNED_EXCEPTION",
    "message": "This prescription has already been scanned and assigned to a user"
}
```

{% endtab %}
{% endtabs %}

Check the `status` of the prescription after executing the API call. If the status is `ACTIVATED` or `NOT_PAID` , the prescription cannot be activated.

Next, activate the prescription.

## Activate a prescription

<mark style="color:blue;">`GET`</mark> `https://api.fibricheck.com/prescriptions/v1/{prescriptionHash}/activate`

#### Path Parameters

| Name             | Type   | Description |
| ---------------- | ------ | ----------- |
| prescriptionHash | String |             |

{% tabs %}
{% tab title="200: OK Successful activation of a prescription" %}

```javascript
{
    "id": "63f632e4fa725244e20d421c",
    "creator_id": "6255af75590801000bdb1c30",
    "reference": "GBX4L5JB",
    "hash": "1f3f4cbb79be1aac595fb07a5e2ac16548ddf9d7",
    "status": "ACTIVATED",
    "group_id": "6258156365220500073f0c7d",
    "user_id": "5811c7a046e0fb000530a465",
    "duration": 604800000,
    "price": 1000,
    "package_id": "5923fbc44cedfd0005042cbb",
    "is_template": false,
    "logs": [
        {
            "old_status": "NOT_PAID",
            "new_status": "FREE",
            "timestamp": 1677079268566
        },
        {
            "old_status": "FREE",
            "new_status": "ACTIVATED",
            "timestamp": 1677079784876
        }
    ],
    "paid_timestamp": 1677079268565,
    "activated_timestamp": 1677079784876,
    "creation_timestamp": 1677079268566,
    "update_timestamp": 1677079784876
}
```

{% endtab %}

{% tab title="400: Bad Request Multiple activations are not allowed" %}

```javascript
{
    "code": 510,
    "name": "WRONG_STATUS_EXCEPTION",
    "message": "This prescription is already activated"
}
```

{% endtab %}
{% endtabs %}

## Get remaining prescription duration

To know the remaining duration of the active prescription, you can request this per group. Execute an API call with the following payload to the endpoint below:&#x20;

```json
{
  "data": {
    "command": "get-remaining-prescription-duration",
    "data": {
      "userId": "{userId}",
      "groupId": "{groupId}"
    }
  }
}
```

## Get remaining prescription duration

<mark style="color:green;">`POST`</mark> `https://api.fibricheck.com/tasks/v1/functions/integration/execute`

#### Request Body

| Name                                   | Type   | Description |
| -------------------------------------- | ------ | ----------- |
| data<mark style="color:red;">\*</mark> | String | payload     |

{% tabs %}
{% tab title="200: OK " %}

```json
{
    "id": "68246ddddee69741463b2b10",
    "functionName": "integration",
    "data": {
        "command": "get-remaining-prescription-duration",
        "data": {
            "userId": "58074849b2148f3b28ad770c",
            "groupId": "618a45a765220500099ff353"
        }
    },
    "status": "complete",
    "createdByApplicationId": "5811ccc246e0fb0006efdc8d",
    "createdByUserId": "58074849b2148f3b28ad770c",
    "priority": 0,
    "startTimestamp": "2025-05-14T10:18:05.577Z",
    "statusChangedTimestamp": "2025-05-14T10:18:07.084Z",
    "updateTimestamp": "2025-05-14T10:18:07.084Z",
    "creationTimestamp": "2025-05-14T10:18:05.577Z",
    "result": {
        "group_id": "618a45a765220500099ff353",
        "start_timestamp": 1741915735420,
        "end_timestamp": 1836610135420
    }
}
```

{% endtab %}
{% endtabs %}

The `result.endTimestamp` in the response will include the prescription's ending time.

## Cancel prescription

A prescription can always be canceled by executing the following API call:

```json
{  
    "data": {
        "command": "cancel-prescription",
        "data": {
          "userId": "{userId}",
          "groupId": "{groupId}"
        }
    }
}
```

## Cancel subscription

<mark style="color:green;">`POST`</mark> `https://api.fibricheck.com/tasks/v1/functions/integration/execute`

#### Request Body

| Name   | Type   | Description |
| ------ | ------ | ----------- |
| data\* | String | payload     |

{% tabs %}
{% tab title="200: OK " %}

{% endtab %}

{% tab title="400: Bad Request" %}

```json
{
    "runtimeError": {
        "name": "period-already-cancelled",
        "message": "The period for group 618a45a765220500099ff353 and user 58074849b2148f3b28ad770c has already been cancelled."
    },
    "taskId": "68501c8da3d6b881aeb9f183",
    "code": 1406,
    "name": "LAMBDA_RUNTIME_EXCEPTION",
    "message": "Invoking the Lambda function resulted in a runtime error."
}
```

{% endtab %}

{% tab title="400: Bad Request" %}

```json
{
    "runtimeError": {
        "name": "cancel-period-failed",
        "message": "Impossible to cancel period for 618a45a765220500099ff352 for user 58074849b2148f3b28ad770c!"
    },
    "taskId": "6854215aa3d6b8dbd3ba274b",
    "code": 1406,
    "name": "LAMBDA_RUNTIME_EXCEPTION",
    "message": "Invoking the Lambda function resulted in a runtime error."
}
```

{% endtab %}
{% endtabs %}
