JavaScript User-Client

This package provides a convenient interface to query the user-authenticated MagicBell API.

Requirements

We support all evergreen browsers. You might need to apply polyfills if you need to support older browsers. Issues in evergreen browsers are considered bugs. Please let us know if you run into any.

Installation

Install the package with npm:

shell
npm install `@magicbell/user-client` --save

or yarn:

shell
yarn add `@magicbell/user-client`

Usage

The client needs to be initialized with a user token. User tokens should be generated on your backend, using your project token. Project tokens can be generated on the MagicBell dashboard, via settings > access tokens.

js
import { Client } from '@magicbell/user-client';

const client = new Client({ accessToken: userToken });

await client.channels.saveWebPushToken({ ... });

saveMobilePushApnsToken

  • HTTP Method: POST
  • Endpoint: /channels/mobile_push/apns/tokens

Required Parameters

| input | object | Request body. |

Return Type

ApnsToken

Example Usage Code Snippet

Typescript
import { Client } from '@magicbell/user-client';

const sdk = new Client({ accessToken: process.env.CLIENT_ACCESS_TOKEN });

(async () => {
  const input = {
    device_token:
      'eH0fLhuiRj2Np7UQ-opXAm:APA91bGtC-wH4sgW1jWkMKIZf7FYkm_RTQb7Jid7DfSJnCgivGYoRzhLrGxpcIF6yPjmbzAr6CKF-6phZkBasFUUfZmfdgcqfA_ZlZdVk6pSnon3LGzMumCzEJE0zgWoo_RUmVUVJUAt',
    installation_id: 'development',
  };
  const result = await sdk.channels.saveMobilePushApnsToken(input);
  console.log(result);
})();

getMobilePushApnsTokens

  • HTTP Method: GET
  • Endpoint: /channels/mobile_push/apns/tokens

Return Type

ArrayWithMetadataOfApnsToken

Example Usage Code Snippet

Typescript
import { Client } from '@magicbell/user-client';

const sdk = new Client({ accessToken: process.env.CLIENT_ACCESS_TOKEN });

(async () => {
  const result = await sdk.channels.getMobilePushApnsTokens();
  console.log(result);
})();

getMobilePushApnsToken

  • HTTP Method: GET
  • Endpoint: /channels/mobile_push/apns/tokens/{token_id}

Required Parameters

NameTypeDescription
tokenIdstring

Return Type

ApnsTokenWithMetadata

Example Usage Code Snippet

Typescript
import { Client } from '@magicbell/user-client';

const sdk = new Client({ accessToken: process.env.CLIENT_ACCESS_TOKEN });

(async () => {
  const result = await sdk.channels.getMobilePushApnsToken('token_id');
  console.log(result);
})();

discardMobilePushApnsToken

  • HTTP Method: DELETE
  • Endpoint: /channels/mobile_push/apns/tokens/{token_id}

Required Parameters

NameTypeDescription
tokenIdstring

Return Type

DiscardResult

Example Usage Code Snippet

Typescript
import { Client } from '@magicbell/user-client';

const sdk = new Client({ accessToken: process.env.CLIENT_ACCESS_TOKEN });

(async () => {
  const result = await sdk.channels.discardMobilePushApnsToken('token_id');
  console.log(result);
})();

saveMobilePushFcmToken

  • HTTP Method: POST
  • Endpoint: /channels/mobile_push/fcm/tokens

Required Parameters

| input | object | Request body. |

Return Type

FcmToken

Example Usage Code Snippet

Typescript
import { Client } from '@magicbell/user-client';

const sdk = new Client({ accessToken: process.env.CLIENT_ACCESS_TOKEN });

(async () => {
  const input = {
    device_token:
      'eH0fLhuiRj2Np7UQ-opXAm:APA91bGtC-wH4sgW1jWkMKIZf7FYkm_RTQb7Jid7DfSJnCgivGYoRzhLrGxpcIF6yPjmbzAr6CKF-6phZkBasFUUfZmfdgcqfA_ZlZdVk6pSnon3LGzMumCzEJE0zgWoo_RUmVUVJUAt',
    installation_id: 'development',
  };
  const result = await sdk.channels.saveMobilePushFcmToken(input);
  console.log(result);
})();

getMobilePushFcmTokens

  • HTTP Method: GET
  • Endpoint: /channels/mobile_push/fcm/tokens

Return Type

ArrayWithMetadataOfFcmToken

Example Usage Code Snippet

Typescript
import { Client } from '@magicbell/user-client';

const sdk = new Client({ accessToken: process.env.CLIENT_ACCESS_TOKEN });

(async () => {
  const result = await sdk.channels.getMobilePushFcmTokens();
  console.log(result);
})();

getMobilePushFcmToken

  • HTTP Method: GET
  • Endpoint: /channels/mobile_push/fcm/tokens/{token_id}

Required Parameters

NameTypeDescription
tokenIdstring

Return Type

FcmTokenWithMetadata

Example Usage Code Snippet

Typescript
import { Client } from '@magicbell/user-client';

const sdk = new Client({ accessToken: process.env.CLIENT_ACCESS_TOKEN });

(async () => {
  const result = await sdk.channels.getMobilePushFcmToken('token_id');
  console.log(result);
})();

discardMobilePushFcmToken

  • HTTP Method: DELETE
  • Endpoint: /channels/mobile_push/fcm/tokens/{token_id}

Required Parameters

NameTypeDescription
tokenIdstring

Return Type

DiscardResult

Example Usage Code Snippet

Typescript
import { Client } from '@magicbell/user-client';

const sdk = new Client({ accessToken: process.env.CLIENT_ACCESS_TOKEN });

(async () => {
  const result = await sdk.channels.discardMobilePushFcmToken('token_id');
  console.log(result);
})();

saveSlackToken

  • HTTP Method: POST
  • Endpoint: /channels/slack/tokens

Required Parameters

| input | object | Request body. |

Return Type

SlackToken

Example Usage Code Snippet

Typescript
import { Client } from '@magicbell/user-client';

const sdk = new Client({ accessToken: process.env.CLIENT_ACCESS_TOKEN });

(async () => {
  const input = { webhook: { url: 'https://example.com/webhook' } };
  const result = await sdk.channels.saveSlackToken(input);
  console.log(result);
})();

getSlackTokens

  • HTTP Method: GET
  • Endpoint: /channels/slack/tokens

Return Type

ArrayWithMetadataOfSlackToken

Example Usage Code Snippet

Typescript
import { Client } from '@magicbell/user-client';

const sdk = new Client({ accessToken: process.env.CLIENT_ACCESS_TOKEN });

(async () => {
  const result = await sdk.channels.getSlackTokens();
  console.log(result);
})();

getSlackToken

  • HTTP Method: GET
  • Endpoint: /channels/slack/tokens/{token_id}

Required Parameters

NameTypeDescription
tokenIdstring

Return Type

SlackTokenWithMetadata

Example Usage Code Snippet

Typescript
import { Client } from '@magicbell/user-client';

const sdk = new Client({ accessToken: process.env.CLIENT_ACCESS_TOKEN });

(async () => {
  const result = await sdk.channels.getSlackToken('token_id');
  console.log(result);
})();

discardSlackToken

  • HTTP Method: DELETE
  • Endpoint: /channels/slack/tokens/{token_id}

Required Parameters

NameTypeDescription
tokenIdstring

Return Type

DiscardResult

Example Usage Code Snippet

Typescript
import { Client } from '@magicbell/user-client';

const sdk = new Client({ accessToken: process.env.CLIENT_ACCESS_TOKEN });

(async () => {
  const result = await sdk.channels.discardSlackToken('token_id');
  console.log(result);
})();

saveTeamsToken

  • HTTP Method: POST
  • Endpoint: /channels/teams/tokens

Required Parameters

| input | object | Request body. |

Return Type

Returns a dict object.

Example Usage Code Snippet

Typescript
import { Client } from '@magicbell/user-client';

const sdk = new Client({ accessToken: process.env.CLIENT_ACCESS_TOKEN });

(async () => {
  const input = {};
  const result = await sdk.channels.saveTeamsToken(input);
  console.log(result);
})();

getTeamsTokens

  • HTTP Method: GET
  • Endpoint: /channels/teams/tokens

Return Type

ArrayWithMetadataOfTeamsToken

Example Usage Code Snippet

Typescript
import { Client } from '@magicbell/user-client';

const sdk = new Client({ accessToken: process.env.CLIENT_ACCESS_TOKEN });

(async () => {
  const result = await sdk.channels.getTeamsTokens();
  console.log(result);
})();

getTeamsToken

  • HTTP Method: GET
  • Endpoint: /channels/teams/tokens/{token_id}

Required Parameters

NameTypeDescription
tokenIdstring

Return Type

TeamsTokenWithMetadata

Example Usage Code Snippet

Typescript
import { Client } from '@magicbell/user-client';

const sdk = new Client({ accessToken: process.env.CLIENT_ACCESS_TOKEN });

(async () => {
  const result = await sdk.channels.getTeamsToken('token_id');
  console.log(result);
})();

discardTeamsToken

  • HTTP Method: DELETE
  • Endpoint: /channels/teams/tokens/{token_id}

Required Parameters

NameTypeDescription
tokenIdstring

Return Type

DiscardResult

Example Usage Code Snippet

Typescript
import { Client } from '@magicbell/user-client';

const sdk = new Client({ accessToken: process.env.CLIENT_ACCESS_TOKEN });

(async () => {
  const result = await sdk.channels.discardTeamsToken('token_id');
  console.log(result);
})();

saveWebPushToken

  • HTTP Method: POST
  • Endpoint: /channels/web_push/tokens

Required Parameters

| input | object | Request body. |

Return Type

WebPushToken

Example Usage Code Snippet

Typescript
import { Client } from '@magicbell/user-client';

const sdk = new Client({ accessToken: process.env.CLIENT_ACCESS_TOKEN });

(async () => {
  const input = {
    endpoint: 'https://example.com/webhook',
    keys: { auth: '', p256dh: '' },
  };
  const result = await sdk.channels.saveWebPushToken(input);
  console.log(result);
})();

getWebPushTokens

  • HTTP Method: GET
  • Endpoint: /channels/web_push/tokens

Return Type

ArrayWithMetadataOfWebPushToken

Example Usage Code Snippet

Typescript
import { Client } from '@magicbell/user-client';

const sdk = new Client({ accessToken: process.env.CLIENT_ACCESS_TOKEN });

(async () => {
  const result = await sdk.channels.getWebPushTokens();
  console.log(result);
})();

getWebPushToken

  • HTTP Method: GET
  • Endpoint: /channels/web_push/tokens/{token_id}

Required Parameters

NameTypeDescription
tokenIdstring

Return Type

WebPushTokenWithMetadata

Example Usage Code Snippet

Typescript
import { Client } from '@magicbell/user-client';

const sdk = new Client({ accessToken: process.env.CLIENT_ACCESS_TOKEN });

(async () => {
  const result = await sdk.channels.getWebPushToken('token_id');
  console.log(result);
})();

discardWebPushToken

  • HTTP Method: DELETE
  • Endpoint: /channels/web_push/tokens/{token_id}

Required Parameters

NameTypeDescription
tokenIdstring

Return Type

DiscardResult

Example Usage Code Snippet

Typescript
import { Client } from '@magicbell/user-client';

const sdk = new Client({ accessToken: process.env.CLIENT_ACCESS_TOKEN });

(async () => {
  const result = await sdk.channels.discardWebPushToken('token_id');
  console.log(result);
})();

saveSlackInstallation

  • HTTP Method: POST
  • Endpoint: /integrations/slack/installations

Required Parameters

| input | object | Request body. |

Return Type

SlackInstallation

Example Usage Code Snippet

Typescript
import { Client } from '@magicbell/user-client';

const sdk = new Client({ accessToken: process.env.CLIENT_ACCESS_TOKEN });

(async () => {
  const input = {
    access_token: 'xoxb-123456789012-1234567890123-12345678901234567890abcdef123456',
    app_id: 'A12345678',
    authed_user: { scope: 'identify,commands' },
    bot_user_id: 'U12345678',
    enterprise_id: 'E12345678',
    enterprise_name: 'Enterprise Grid, Inc.',
    incoming_webhook: {
      channel: 'C12345678',
      channel_id: 'C12345678',
      configuration_url: 'https://teamname.slack.com/services/B12345678',
      url: 'https://hooks.slack.com/services/T12345678/B12345678/123456789012345678901234',
    },
    scope: 'identify,commands,bot',
    team: { name: 'Team Installing Your App' },
    team_id: 'T12345678',
    team_name: 'Team Installing Your App',
  };
  const result = await sdk.integrations.saveSlackInstallation(input);
  console.log(result);
})();

finishSlackInstallation

  • HTTP Method: POST
  • Endpoint: /integrations/slack/installations/finish

Required Parameters

| input | object | Request body. |

Return Type

SlackInstallation

Example Usage Code Snippet

Typescript
import { Client } from '@magicbell/user-client';

const sdk = new Client({ accessToken: process.env.CLIENT_ACCESS_TOKEN });

(async () => {
  const input = { app_id: '12345678901', code: 'string', redirect_url: 'string' };
  const result = await sdk.integrations.finishSlackInstallation(input);
  console.log(result);
})();

startSlackInstallation

  • HTTP Method: POST
  • Endpoint: /integrations/slack/installations/start

Required Parameters

| input | object | Request body. |

Return Type

Returns a dict object.

Example Usage Code Snippet

Typescript
import { Client } from '@magicbell/user-client';

const sdk = new Client({ accessToken: process.env.CLIENT_ACCESS_TOKEN });

(async () => {
  const input = {
    app_id: '12345678901',
    auth_url: 'https://example.com/auth',
    extra_scopes: ['scope1', 'scope2'],
    redirect_url: 'https://example.com/redirect',
  };
  const result = await sdk.integrations.startSlackInstallation(input);
  console.log(result);
})();

saveTemplatesInstallation

  • HTTP Method: POST
  • Endpoint: /integrations/templates/installations

Required Parameters

| input | object | Request body. |

Return Type

TemplatesInstallation

Example Usage Code Snippet

Typescript
import { Client } from '@magicbell/user-client';

const sdk = new Client({ accessToken: process.env.CLIENT_ACCESS_TOKEN });

(async () => {
  const input = {
    apns: {
      app_id: 'com.example.myapp',
      badge: 'unread',
      certificate: 'MIICXQIBAAKBgQC3J2',
      key_id: 'ABCD1234EF',
      team_id: 'ABCD1234EF',
    },
  };
  const result = await sdk.integrations.saveTemplatesInstallation(input);
  console.log(result);
})();

startWebPushInstallation

  • HTTP Method: POST
  • Endpoint: /integrations/web_push/installations/start

Required Parameters

| input | object | Request body. |

Return Type

WebPushStartInstallationResponse

Example Usage Code Snippet

Typescript
import { Client } from '@magicbell/user-client';

const sdk = new Client({ accessToken: process.env.CLIENT_ACCESS_TOKEN });

(async () => {
  const input = {
    imports: [],
    originalName: 'start_web_push_installation_request',
    services: ['integrations'],
    filePath: 'src/services/integrations/models',
    modelName: 'StartWebPushInstallationRequest',
    title: 'StartWebPushInstallationRequest',
  };
  const result = await sdk.integrations.startWebPushInstallation(input);
  console.log(result);
})();

Support

New features and bug fixes are released on the latest major version of the @magicbell/user-client package. If you are on an older major version, we recommend that you upgrade to the latest in order to use the new features and bug fixes including those for security vulnerabilities. Older major versions of the package will continue to be available for use, but will not be receiving any updates.