> For the complete documentation index, see [llms.txt](https://docs.powens.com/documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.powens.com/documentation/sdk/powens-webview-js.md).

# Powens Webview JS

Learn how to integrate our JavaScript and TypeScript Webview SDK into your web application.

Powens Webview JS is the official JavaScript and TypeScript SDK for presenting the [Powens Webview](https://docs.powens.com/api-reference/overview/webview) from a web application. It is published on npm as `@powenscompany/webview-js` and distributed under the LGPL-3.0 licence.

The SDK is a Web Component. You add a `<powens-webview>` element to your page, set its `options` property and call `openWebview()`; the SDK builds the webview URL, presents it, and hands you the result when the user finishes.

It supports the four webview flows — connect, reconnect, manage and payment — and seven interface languages.

You can try it out [here](https://integrate.powens.com/demos/sdk).

## Installation

### npm

```bash
npm install @powenscompany/webview-js
```

TypeScript types are included; there is no separate `@types/` package.

### CDN

No build step required:

```html
<script type="module" src="https://unpkg.com/@powenscompany/webview-js"></script>
```

## Usage

Declare the element, set the options, open it. The options are read when `openWebview()` is called, so set them first.

```html
<!doctype html>
<html lang="en">
<head>
  <script type="module" src="https://unpkg.com/@powenscompany/webview-js"></script>
  <script type="module" src="app.js"></script>
</head>
<body>
  <button id="open-webview">Connect your bank</button>
  <powens-webview></powens-webview>
</body>
</html>
```

```javascript
// app.js
const powensWebview = document.querySelector('powens-webview');

document.getElementById('open-webview').addEventListener('click', () => {
  powensWebview.options = {
    flow: 'connect',
    domain: 'yourdomain.biapi.pro',
    clientId: 'YOUR_CLIENT_ID',
    redirectUri: window.location.origin,
    lang: 'en',
  };
  powensWebview.openWebview();
});
```

### Handling the result

When the user completes or dismisses a flow, the SDK dispatches a `message` event on `window` with the type `powensWebviewTermination`. Always check the origin before trusting it.

```javascript
window.addEventListener('message', (event) => {
  if (event.origin !== window.origin) return;
  if (event.data.type !== 'powensWebviewTermination') return;

  const result = event.data.data;
  console.log('Connection ID:', result.connectionId);
});
```

### Framework setup

The element works the same everywhere; only the wiring differs. Each framework needs one thing done once.

{% tabs %}
{% tab title="TypeScript" %}
Import the element type and the enums, then cast the element:

```typescript
import PowensWebviewElement, {
  PowensWebviewFlow,
  PowensWebviewLanguage,
  PowensWebviewMessage,
} from '@powenscompany/webview-js';

const powensWebview = document.querySelector('powens-webview') as PowensWebviewElement;

powensWebview.options = {
  flow: PowensWebviewFlow.Connect,
  domain: 'yourdomain.biapi.pro',
  clientId: 'YOUR_CLIENT_ID',
  redirectUri: window.location.origin,
  lang: PowensWebviewLanguage.English,
};

powensWebview.openWebview();
```

{% endtab %}

{% tab title="React" %}
Declare the custom element once, in `src/vite-env.d.ts`, or JSX will not accept it:

```tsx
/// <reference types="vite/client" />

import type { DetailedHTMLProps, HTMLAttributes, RefObject } from 'react';
import type PowensWebviewElement from '@powenscompany/webview-js';

type PowensWebviewProps = DetailedHTMLProps<HTMLAttributes<PowensWebviewElement>, PowensWebviewElement> & {
  ref?: RefObject<PowensWebviewElement | null>;
};

declare global {
  namespace React.JSX {
    interface IntrinsicElements {
      'powens-webview': PowensWebviewProps;
    }
  }
}
```

Then reach the element through a ref:

```tsx
const webviewRef = useRef<PowensWebviewElement>(null);

const open = () => {
  webviewRef.current!.options = { /* ... */ };
  webviewRef.current!.openWebview();
};

return <powens-webview ref={webviewRef} />;
```

Remember to remove the `message` listener on unmount.
{% endtab %}

{% tab title="Angular" %}
Add `CUSTOM_ELEMENTS_SCHEMA` to the component, or the template will reject the element:

```typescript
@Component({
  selector: 'app-root',
  standalone: true,
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
  template: `
    <button (click)="open()">Connect your bank</button>
    <powens-webview #webview></powens-webview>
  `,
})
export class AppComponent {
  @ViewChild('webview') webview!: ElementRef<PowensWebviewElement>;

  open(): void {
    this.webview.nativeElement.options = { /* ... */ };
    this.webview.nativeElement.openWebview();
  }
}
```

{% endtab %}
{% endtabs %}

Complete, runnable projects for all three are in the [examples folder](https://github.com/powenscompany/powens-webview-js/tree/main/examples) of the repository.

## Methods

| Method           | Description                                                                                                    |
| ---------------- | -------------------------------------------------------------------------------------------------------------- |
| `openWebview()`  | Builds the webview URL from `options` and presents it. Does nothing if the webview is already open.            |
| `closeWebview()` | Closes the webview and removes its event listeners. Called automatically once the termination event has fired. |

If the user clicks outside the webview while a flow is in progress, the SDK asks for confirmation before closing. This is built in and needs no configuration.

## Options

`flow`, `domain` and `clientId` are required for every flow. Which of the others apply depends on the flow; see the [Webview endpoints reference](https://docs.powens.com/api-reference/overview/webview#endpoints-reference) for what each one does.

| Option                      | Type                    | Description                                                                                                                |
| --------------------------- | ----------------------- | -------------------------------------------------------------------------------------------------------------------------- |
| `flow`                      | `PowensWebviewFlow`     | The flow to open. Required.                                                                                                |
| `domain`                    | String                  | Your Powens API domain, for example `yourdomain.biapi.pro`. Required.                                                      |
| `clientId`                  | String                  | Your application's client ID. Required.                                                                                    |
| `lang`                      | `PowensWebviewLanguage` | Interface language. Omitted, the webview picks one.                                                                        |
| `code`                      | String                  | User-scoped temporary token. On the connect flow, omitting it creates an anonymous user and returns a token in the result. |
| `redirectUri`               | String                  | Absolute URL the webview returns to, typically `window.location.origin`.                                                   |
| `state`                     | String                  | Opaque value echoed back unchanged in the result. Useful to correlate a flow with a request of your own.                   |
| `connectorIds`              | Array of numbers        | Restrict the connectors offered, by ID.                                                                                    |
| `connectorUuids`            | Array of strings        | Restrict the connectors offered, by UUID.                                                                                  |
| `connectorCapabilities`     | Array of strings        | Restrict the connectors offered by capability, for example `bank`, `bankwealth`, `document`.                               |
| `connectorFieldValues`      | Object                  | Pre-fill connector login fields. Keyed by connector UUID, then by field name.                                              |
| `accountTypes`              | Array of strings        | Restrict the account types shown, for example `checking`, `card`, `market`, `perco`.                                       |
| `accountIbans`              | Array of strings        | Restrict account selection to specific IBANs.                                                                              |
| `connectionId`              | Number                  | The connection to act on. Used by the reconnect and manage flows.                                                          |
| `connectionSources`         | Array of strings        | Target specific connection sources.                                                                                        |
| `resetCredentials`          | `true`                  | On the reconnect flow, force a full credential reset instead of a simple re-authentication.                                |
| `paymentId`                 | Number                  | The payment to confirm. Required by the payment flow.                                                                      |
| `notifyAndCloseExternalTab` | `true`                  | Notify the opener and close the tab at the end of the flow.                                                                |
| `maxConnections`            | Number                  | **Deprecated.**                                                                                                            |

## Result

The `data` property of the termination message is a `PowensWebviewResult`. Which fields are present depends on the flow and on how it ended.

| Field               | Type             | Description                                                                    |
| ------------------- | ---------------- | ------------------------------------------------------------------------------ |
| `connectionId`      | Number           | ID of the connection that was created.                                         |
| `connectionIds`     | Array of numbers | IDs of all connections created, when several were allowed in one session.      |
| `connectionDeleted` | Boolean          | `true` when the user deleted a connection during a manage flow.                |
| `code`              | String           | A new user token, returned by the connect flow when no `code` was passed in.   |
| `state`             | String           | Echo of the `state` option.                                                    |
| `error`             | String           | Error code when the flow failed or was cancelled, for example `access_denied`. |
| `errorDescription`  | String           | Human-readable explanation of the error.                                       |

Treat any non-empty `error` as a failure: further codes may be added over time.

## Types

```typescript
enum PowensWebviewFlow {
  Connect = 'connect',
  Reconnect = 'reconnect',
  Manage = 'manage',
  Payment = 'payment',
}

enum PowensWebviewLanguage {
  English = 'en',
  French = 'fr',
  German = 'de',
  Dutch = 'nl',
  Italian = 'it',
  Spanish = 'es',
  Portuguese = 'pt',
}

interface PowensWebviewMessage {
  type: 'powensWebviewTermination';
  data: PowensWebviewResult;
}
```

## Notes

* **Set `options` before calling `openWebview()`.** They are read at call time, not when assigned.
* **Check `event.origin`** in your message handler before acting on the result.
* **The payment flow returns elsewhere on success.** The bank redirects to the `client_redirect_uri` given when the payment was created through the API; the `redirectUri` option is used only for errors and cancellations.
* **One user token per flow.** The `code` option is a user-scoped temporary token, not your client secret. Never put a client secret in front-end code.

## Resources

* [Interactive demo](https://integrate.powens.com/demos/sdk)
* [npm — @powenscompany/webview-js](https://www.npmjs.com/package/@powenscompany/webview-js)
* [GitHub — powenscompany/powens-webview-js](https://github.com/powenscompany/powens-webview-js)
* [Webview reference](https://docs.powens.com/api-reference/overview/webview)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.powens.com/documentation/sdk/powens-webview-js.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
