> ## Documentation Index
> Fetch the complete documentation index at: https://docs.refold.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Javascript SDK

<Tip>
  You can refer to the Refold's GitHub repo for Javascript SDK [here](https://gocobalt.github.io/cobalt-js/).
</Tip>

#### Install

First, install `@cobaltio/cobalt-js`  and via the terminal.

```bash theme={null}
npm install --save  @cobaltio/cobalt-js
```

#### Browser

```html theme={null}
<!-- use this if you get an error saying `exports` is not defined. -->
<script>var exports = {};</script>
<!-- use a specific version -->
<script src="https://cdn.jsdelivr.net/npm/@cobaltio/cobalt-js@8.0.0"></script>
<!-- use a version range instead of a specific version -->
<script src="https://cdn.jsdelivr.net/npm/@cobaltio/cobalt-js@8"></script>
<script src="https://cdn.jsdelivr.net/npm/@cobaltio/cobalt-js@8.0"></script>
<!-- omit the version completely to use the latest one -->
<!-- you should NOT use this in production -->
<script src="https://cdn.jsdelivr.net/npm/@cobaltio/cobalt-js"></script>
```

#### Initialise

Import `Cobalt` and initialise it.

```JavaScript theme={null}
import { Cobalt } from "@cobaltio/cobalt-js";

// or, if you're using CommonJS
const { Cobalt } = require("@cobaltio/cobalt-js");

// initialize with token
const cobalt = new Cobalt({
    // the token you generate for linked accounts using the cobalt backend SDK
    token: "COBALT_SESSION_TOKEN",
});

// Or, initialize without token
const cobalt = new Cobalt();
// and you can set the token later.
cobalt.token = "COBALT_SESSION_TOKEN";
```

#### .getApp()

Returns the application details for the specified application, provided the application is enabled in Refold. If no application is specified, it returns
all the enabled applications.

| Param   | Required | Type   | Description          |
| ------- | -------- | ------ | -------------------- |
| \[slug] | optional | String | The application slug |

> Note: instance method of `Cobalt` Returns: `Promise.<Application>` - The application details.

```JavaScript theme={null}
cobalt.getApp()  // to get a list of all apps
cobalt.getApp("slack")  // to get details of a single app, e.g. slack
```

Example Response:

```JSON theme={null}
{
	"name": "Slack",
	"icon": "https://cobalt-app-logos.s3.ap-south-1.amazonaws.com/slack/logo.png",
	"description": "Slack is a platform for team communication: everything in one place, instantly searchable, available wherever you go. Offering instant messaging, document sharing and knowledge search for modern teams.",
	"auth_type": "oauth2",
	"type": "slack",
	"app_id": "64c357c739ec788238ab5f95",
	"tags": [
		"Communication"
	],
	"version": {
		"_v": "1.0.0",
		"description": "Slack is a platform for team communication: everything in one place, instantly searchable, available wherever you go. Offering instant messaging, document sharing and knowledge search for modern teams."
	},
	"slug": "slack",
	"reauth_required": false
}
```

#### .connect()

Returns the application details for the specified application, provided the application is enabled in Refold. If no application is specified, it returns all the enabled applications.

| Param   | Required  | Type   | Description                      |
| ------- | --------- | ------ | -------------------------------- |
| \[slug] | Mandatory | String | The application slug             |
| Payload | Optional  | Object | The key value pairs of auth data |

> Note: instance method of `Cobalt` Returns: `Promise.<Application>` - The application details.

```JavaScript theme={null}
cobalt.connect("slack") // opens a new tab for oauth apps
cobalt.connect("twilio", {
  number: 1234567890,
  sid: "ACCOUNT_SID",
}) // saves the auth data for key based apps
```

```response theme={null}
// returns `true` if connection was successful
```

#### .disconnect()

Disconnect the specified application and remove any associated data from Refold.

| Param   | Required  | Type   | Description          |
| ------- | --------- | ------ | -------------------- |
| \[slug] | Mandatory | String | The application slug |

```JavaScript theme={null}
cobalt.disconnect("slack")
```

```
// no response
```

#### .config()

Returns the specified config, or creates one if it doesn't exist.

| Param   | Required  | Type            | Description             |
| ------- | --------- | --------------- | ----------------------- |
| payload | Mandatory | `configPayload` | Json payload for config |

The `configPayload` object.

| Name          | Type                            | Description                |
| ------------- | ------------------------------- | -------------------------- |
| slug          | String                          | The application slug       |
| \[config\_id] | String                          | Unique id for the config   |
| labels        | `Object<string, Array<labels>>` | The dynamic label mappings |

```JavaScript theme={null}
cobalt.config({
  slug: "slack",
  config_id: "OPTIONAL_ID_FOR_THIS_CONFIG",
  labels: {}, // optional dynamic labels
})
```

Example response:

```Json theme={null}
{
	"slug": "slack",
	"config_id": "OPTIONAL_ID_FOR_THIS_CONFIG",
	"fields": [
		{
			"required": false,
			"id": "64da0b57c9ae95561bb0a24d",
			"name": "Channel",
			"field_type": "select",
			"options": [
				{
					"name": "general",
					"value": "C044U7Q074J"
				}
			],
			"labels": []
		}
	],
	"workflows": [
		{
			"id": "64d1fac58716dc5065127ffe",
			"name": "Send message in a channel",
			"description": "",
			"enabled": false,
			"fields": [
				{
					"required": false,
					"id": "64da0b57c9ae95561bb0a24f",
					"name": "Channel",
					"field_type": "select",
					"options": [
						{
							"name": "general",
							"value": "C044U7Q074J"
						}
					],
					"labels": []
				}
			]
		}
	],
	"field_errors": []
}
```

#### .updateConfig()

Update the specified config.

| Param   | Required  | Type                  | Description             |
| ------- | --------- | --------------------- | ----------------------- |
| payload | Mandatory | `updateConfigPayload` | Json payload for config |

The `updateConfigPayload` configuration data for an application.

| Name          | Required  | Type                      | Description                                   |
| ------------- | --------- | ------------------------- | --------------------------------------------- |
| slug          | Mandatory | String                    | The application slug.                         |
| \[config\_id] | Optional  | String                    | Unique ID for the config.                     |
| fields        | Optional  | Object                    | A map of application fields and their values. |
| workflows     | Optional  | `Array.<WorkflowPayload>` | Whether the workflow is enabled.              |

The `WorkflowPayload` data

| Name     | Required  | Type                                            | Description                                |
| -------- | --------- | ----------------------------------------------- | ------------------------------------------ |
| id       | Mandatory | String                                          | The ID of the workflow.                    |
| enabled] | Mandatory | Boolean                                         | Whether the workflow is enabled            |
| fields   | Mandatory | `Object<string, (string or number or boolean)>` | A map of workflow fields and their values. |

```JavaScript theme={null}
cobalt.updateConfig({
  slug: "slack",
  config_id: "OPTIONAL_ID_FOR_THIS_CONFIG",
  fields: {
    "64da0b57c9ae95561bb0a24d": "C044U7Q074J"
  },
  workflows: [
    {
      id: "64d1fac58716dc5065127ffe",
      enabled: true,
      fields: {
        "64da0b57c9ae95561bb0a24f": "C044U7Q074J"
      }

    }
  ]
})
```

Example response:

```JSON theme={null}
{
	"slug": "slack",
	"config_id": "OPTIONAL_ID_FOR_THIS_CONFIG",
	"fields": [
		{
			"required": false,
			"id": "64da0b57c9ae95561bb0a24d",
			"name": "Channel",
			"field_type": "select",
			"options": [
				{
					"name": "general",
					"value": "C044U7Q074J"
				}
			],
			"labels": [],
			value: "C044U7Q074J"
		}
	],
	"workflows": [
		{
			"id": "64d1fac58716dc5065127ffe",
			"name": "Send message in a channel",
			"description": "",
			"enabled": false,
			"fields": [
				{
					"required": false,
					"id": "64da0b57c9ae95561bb0a24f",
					"name": "Channel",
					"field_type": "select",
					"options": [
						{
							"name": "general",
							"value": "C044U7Q074J"
						}
					],
					"labels": [],
			        value: "C044U7Q074J"
				}
			]
		}
	],
	"field_errors": []
}
```

#### .deleteConfig() 

Delete the specified config.

| Param    | Required  | Type   | Description                  |
| -------- | --------- | ------ | ---------------------------- |
| slug     | Mandatory | slug   | The application slug.        |
| configId | optional  | String | The unique ID of the config. |

```JavaScript theme={null}
cobalt.deleteConfig("slack", "OPTIONAL_ID_FOR_THIS_CONFIG")
```

```
// promise resolves if config is successfully deleted
```
