# blip-iframe

> A type safe and intuitive API to communicate with the Blip platform

Latest version **1.7.5** (published 2024-08-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install blip-iframe
pnpm add blip-iframe
yarn add blip-iframe
bun add blip-iframe
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.7.5 |
| Published | 2024-08-05 |
| First published | 2023-07-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 240.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Heloi Neto |
| Maintainers | heloineto |
| Keywords | blip, chatbot, iframe, sdk |

## Links

- npm: https://www.npmjs.com/package/blip-iframe
- Repository: https://github.com/heloineto/blip-iframe
- Homepage: https://github.com/heloineto/blip-iframe#readme
- Issues: https://github.com/heloineto/blip-iframe/issues
- npm.io page: https://npm.io/package/blip-iframe

## Dependencies (1)

- [iframe-message-proxy](https://npm.io/package/iframe-message-proxy.md) ^1.2.0

## Recent versions

- 1.7.5 (latest) — 2024-08-05
- 1.7.4 — 2024-05-13
- 1.6.4 — 2024-05-02
- 1.5.4 — 2024-04-30
- 1.5.3 — 2024-04-30
- 1.5.2 — 2024-04-30
- 1.4.2 — 2024-04-23
- 1.4.1 — 2024-04-22
- 1.4.0 — 2024-04-19
- 1.3.3 — 2024-04-19
- 1.3.2 — 2024-04-19
- 1.3.1 — 2024-04-19
- 1.3.0 — 2024-04-19
- 1.1.2 — 2024-04-19
- 1.2.0 — 2024-04-18
- … 13 more at https://npm.io/package/blip-iframe/versions

## README

# Getting started

Install `blip-iframe` in your project:

```bash
npm i blip-iframe
```

## Usage

You can use `blip-iframe` with any framework or even vanilla JavaScript.
In this section, we'll show how to get started using React, but the same principles
apply to any other tool or framework.

### Usage with Iframe Message Proxy (default)

If you're using `blip-iframe` inside Blip (ex.: Blip Extensions),
using Iframe Message Proxy is the recommended way to go. Luckily, this is
also the default you can use the API commands and actions without any additional configuration,
as such:

```jsx
import { useState, useEffect } from 'react';
import { blip } from 'blip-iframe';

export default function Component() {
  const [application, setApplication] = useState(null);
  const [error, setError] = useState(null);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    // Fetches data about the current bot. If an error occurs, the data will be null.
    // The error is not thrown, but returned. This improves types and error awareness.
    blip.getApplication().then((response) => {
      setLoading(false);
      if (!response.success) {
        setError(response.error);
      } else {
        setApplication(response.data);
      }
    });
  }, []);

  if (loading) return <>Loading...</>;

  if (error) return <>An error ocurred!</>;

  // Shows data about the current bot
  return <pre>{JSON.stringify(application, null, 2)}</pre>;
}
```

Under the hood, `blip-iframe` will send the 'getApplication' action
using the Iframe Message Proxy, and properly type the response.

All set! Now you can use the `blip-iframe` API to make calls inside your application.

> **This method only works if your web app is inside Blip**
>
> Using `blip-iframe` without setting up authentication only works
> in web applications rendered as an iframe inside Blip, because the
> authentication is done automatically by the platform.
>
> If you want to make the same calls in a standalone app,
> follow the steps outlined below.

### Usage with the REST API

To use `blip-iframe` in a standalone application, you need to use the REST API.
In order to do that, you need to create a sender function. This function will
take the commands parameters and send them using whatever method you like, in
this case, the REST API.

This function should be passed as the second parameters of any `blip-iframe`
helper function. See the example below.

#### Sender function using fetch and the REST API

```ts
import type { Message, Sender } from 'blip-iframe';
import { blip } from 'blip-iframe';
import { v4 as uuidv4 } from 'uuid';

const sender: Sender = async <TData = unknown>(message: Message) => {
  if (message.action !== 'sendCommand') {
    return {
      success: false,
      error: new Error("The REST API doesn't support actions, only commands"),
    } as const;
  }

  const response = await fetch('https://msging.net/commands', {
    method: 'POST',
    headers: {
      Authorization: 'Key your-authorization-key',
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ id: uuidv4(), ...message.content.command }),
  });

  if (!response.ok) {
    return { success: false, error: new Error(response.statusText) } as const;
  }

  const { resource } = (await response.json()) as { resource: TData };

  return { success: true, data: resource } as const;
};

// Call any command using the sender function as the second parameter
blip.getTickets({ skip: 0, take: 20 }, sender);
```

### Usage with using the Blip SDK

Coming soon...

---
_Source: https://npm.io/package/blip-iframe · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
