SignalWire Client

View as Markdown

The SignalWire client enables the SignalWire’s vision for Programmable Unified Communications. It allows a new paradigm in communications, designed to streamline usage and development across all communication types.

The following section details the mechanisms available in the SignalWire browser SDK @signalwire/js to take advantage of the unified communication architecture.

Installation

The SignalWire client is available in the SignalWire Browser SDK @signalwire/js versions 3.27.0 and later.

If your project uses a package manager, the Browser SDK can be installed like so:

npm2yarn
$npm install @signalwire/js

It is also available through our CDN and can be included directly into the <head> section of your webpage.

1<script src="https://cdn.signalwire.com/@signalwire/js"></script>

Once installed, you can take advantage of the new capabilities that the SignalWire client enables by instantiating a SignalWire Client, like so:

1<script type="text/javascript" src="https://cdn.signalwire.com/@signalwire/js"></script>
2<script>
3 async function main() {
4 const client = await SignalWire.SignalWire({
5 token: "<TOKEN>",
6 });
7 }
8</script>

The access token is received after the subscriber completes the OAuth2 flow and successfully logs in. Learn more at the Authentication section further below.

Concepts

Resources

Resources are the primary entities for communication within SignalWire. Resources include: Subscribers SWML Scripts, SignalWire AI Agents, Video-Rooms, SIP Endpoints, etc.

Subscribers

Subscribers represent the end-users within SignalWire. They are the endpoints of communication, capable of receiving or initiating calls, messages, and other forms of interaction.

SignalWire allows subscribers to switch from one type of communication to another. For example, switching from a phone call into a video-room to a web-based video call.

You can create Subscribers and Resources in the Resource section of your SignalWire Dashboard, or with a HTTP POST request:

$curl --location --request POST 'https://spacename.signalwire.com/api/fabric/subscribers' \
>--user "project_id:api key" \
>--header 'Content-Type: application/json' \
>--data-raw '{
> "email": "[the subscriber email]",
> "password": "[the subscriber password]"
>}'

Addresses

Each resource and subscriber is uniquely identified by an addresses which can be called, sent messages to, or interacted with in some other way.

The address is composed of two parts: “/context/name

  • Context: A identifier that indicates in which context the resource is located.
  • Name: The name is the unique identifier for the resource.

For example, the address for a Subscribers resource named Alice in the public context would be /public/Alice.

Authentication

As explained in the Subscribers section above, Subscribers represent the end-users within SignalWire, thus usually actions are performed on behalf of the subscriber.

So, when using the SignalWire Client, you will need a Subscriber’s token to authenticate your requests. Usually, that token is obtained when the subscriber logs in using the OAuth2 flow described further below. But your server applications can perform actions on behalf of the subscriber by using getting a token using the REST API.

REST API Token Authentication

You can obtain authentication tokens directly through the REST API. The following endpoints are available for token generation:

Create a Guest Token

Generate a limited-access token for guest access to specific Fabric addresses.

POST
/api/fabric/guests/tokens
1curl -X POST https://{your_space_name}.signalwire.com/api/fabric/guests/tokens \
2 -H "Content-Type: application/json" \
3 -u "<project_id>:<api_token>" \
4 -d '{
5 "allowed_addresses": [
6 "string"
7 ]
8}'

Generate a token to access resources associated with a specific subscriber on behalf of the subscriber.

POST
/api/fabric/subscriber/invites
1curl -X POST https://{your_space_name}.signalwire.com/api/fabric/subscriber/invites \
2 -H "Content-Type: application/json" \
3 -u "<project_id>:<api_token>" \
4 -d '{
5 "address_id": "3fa85f64-5717-4562-b3fc-2c963f66afa6"
6}'

Create a new subscriber and get their token in a single request.

POST
/api/fabric/subscribers/tokens
1curl -X POST https://{your_space_name}.signalwire.com/api/fabric/subscribers/tokens \
2 -H "Content-Type: application/json" \
3 -u "<project_id>:<api_token>" \
4 -d '{
5 "reference": "john.doe@example.com"
6}'

For browser-based applications, we recommend using the OAuth2 flow to authenticate subscribers and obtain tokens. See Authenticate subscribers using OAuth2 below for details.

Using the Token

The endpoints return a JSON object containing a token property. Use this token to initialize the SignalWire Client:

1import { SignalWire } from "@signalwire/js";
2
3const client = await SignalWire({
4 token: "eyJhbGciOiJIUzI1NiIs..."
5});

Authenticate subscribers using OAuth2

When you create a Subscriber, you assign them a username (email) and a password. These credentials can be used authenticate the subscriber using the standard OAuth2 flow with PKCE. For OAuth2, you can use tools like odic-client-ts or react-native-app-auth.

Currently, the only way to get a client ID for the OAuth2 flow is through the SignalWire Support team.

1import { UserManager, WebStorageStateStore } from "oidc-client-ts";
2
3const config = {
4 authority: "x", // dummy authority
5 metadata: {
6 issuer: "https://id.fabric.signalwire.com/",
7 authorization_endpoint: "https://id.fabric.signalwire.com/login/oauth/authorize",
8 token_endpoint: "https://id.fabric.signalwire.com/oauth/token",
9 },
10 client_id: "<Your_Fabric_Client_id>",
11 redirect_uri: "https://redirect_uri",
12 response_type: "code",
13 userStore: new WebStorageStateStore({ store: window.localStorage }),
14};
15
16const userManager = new UserManager(config);
17
18await userManager.signinRedirect();

A complete example is presented below. Assuming that this page is npx served at the address http://localhost:3000, this script takes the subscriber through the OAuth2 login flow, and uses the access token received to fetch the subscriber’s registration details from SignalWire.

index.html
1<html>
2 <head>
3 <style>
4 .loading > button { display: none }
5 .loading::after { content: "Loading..." }
6 </style>
7 </head>
8
9 <body>
10 <div id="container" class="loading">
11 <button onclick="userManager.signinRedirect()">Log In</button>
12 </div>
13
14 <script src="https://cdn.jsdelivr.net/npm/oidc-client-ts@3.0.1/dist/browser/oidc-client-ts.min.js"></script>
15 <script src="https://cdn.signalwire.com/@signalwire/js"></script>
16 <script>
17 const container = document.getElementById("container");
18
19 const config = {
20 authority: "x", // dummy authority
21 metadata: {
22 issuer: "https://id.fabric.signalwire.com/",
23 authorization_endpoint: "https://id.fabric.signalwire.com/login/oauth/authorize",
24 token_endpoint: "https://id.fabric.signalwire.com/oauth/token",
25 },
26 client_id: "<Your_Fabric_Client_id>",
27 redirect_uri: "http://localhost:3000", // assuming this page is being served at port 3000 of localhost
28 response_type: "code",
29 userStore: new oidc.WebStorageStateStore({ store: window.localStorage }),
30 };
31
32 const userManager = new oidc.UserManager(config);
33
34 async function checkForRedirect() {
35 try {
36 const user = await userManager.signinRedirectCallback();
37 if (user) {
38 // the oauth flow completed successfully, we can now use subscriber features using the access token.
39 await getSubscriberInfo(user.access_token);
40 }
41 } catch (e) {
42 // signinRedirectCallback failed; login flow hasn't been started yet. We'll show the Login button.
43 container.classList.remove("loading");
44 }
45 }
46
47 async function getSubscriberInfo(token) {
48 const client = await SignalWire.SignalWire({ token });
49 const subInfo = await client.getSubscriberInfo();
50
51 container.classList.remove("loading");
52 container.innerText = `Hello, ${subInfo.first_name}`;
53 }
54
55 // We want to check if the user was redirected here from the subscriber login flow,
56 // or if the came here directly from the browser. If the user came directly from the
57 // browser, we show a Login button. If they were redirected here, we allow oidc-client-ts
58 // to finish the flow and get the access token.
59 checkForRedirect();
60 </script>
61 </body>
62</html>