# Deploying a CDK connector using CLI

## Overview

You can deploy connectors built through CDK using the [CDK CLI](https://tray.ai/documentation/developer/connector-development-kit/reference/cli-reference#tray-cdk-deployment-create).

## Prerequisites - API token

In order to deploy a connector, you will need an RBAC (Role Based Access Control) API token, [you can follow this guide on how to create one](https://tray.ai/documentation/platform/api-management/api-operations).

### Deploying to different regions

Tray operates in 3 regions: US, EU and APAC. You will need to use the region specific base URL and RBAC token, for each region you wish to use the connector in.

* US: <https://api.tray.io/>
* EU: <https://api.eu1.tray.io/>
* APAC: <https://api.ap1.tray.io/>

## Deployment flow

### 1. Deploy the connector

To deploy the connector, you will need to set two environment variables.

1. Run the following command to set `TRAY_API_URL`:

`export TRAY_API_URL=https://api.tray.io`

> **The above URL (\`https\://api.tray.io\`) is for the US region.** If you are deploying the connector in a different region, use the region specfifc URL.

2. Run the following command to set `TRAY_API_TOKEN`:

`export TRAY_API_TOKEN=<API_TOKEN>`

where API\_TOKEN is the token you created as a [pre-requisite](#prerequisites---api-token) for this step.

With the environment variables set now, you are ready to run the deploy command:

`tray-cdk deployment create`

The command will execute the tests and then deploy the connector. You should see a response similar to the one shown here:

```cli
...
...Test execution logs
...

Ran all test suites.
Tests ran successfully
Connector Build Started
Generating schemas for operation <operation_2_name>
Generating schemas for operation <operation_2_name>
Connector Build Finished
Connector Deploy Started
Connector Deploy Request Sent
Deployment [856b5518-6829-519f-9358-d34b7f848f07] is in progress
```

The connector should be available within the Tray app in a couple of minutes.

The deployment UUID: `856b5518-6829-519f-9358-d34b7f848f07` can be used to check the status as shown in the next step.

### 2. Check deployment status

You can check the status of the deployment using:

`tray-cdk deployment get [CONNECTOR_NAME] [CONNECTOR_VERSION] [UUID]`

where `UUID` is the unique deployment ID you received in the step above and `CONNECTOR_NAME` and `CONNECTOR_VERSION` are the values of name and version keys in the `connector.json` file respectively.

For the deployment shown above, it will be:

`tray-cdk deployment get {connector-name} {connector-version} 856b5518-6829-519f-9358-d34b7f848f07`

Depending on depployment status: `building`, `deploying`, `deployed`, and `failed`, You should see a response as follows:

```cli building
Connector is currently being built, this could take a few minutes. Please check the connector status again later.
```

```cli deploying
Connector is currently being deployed, this could take a few minutes. Please check the connector status again later.
```

```cli deployed
Connector Deployed Successfully! 🎉 🎉 🎉
```

```cli failed
Connector Deploy Failed, please try again or contact support
```

### 3. Share connector with emails

> **You can't share connectors across different Tray organizations.** You can also use the [API](https://tray.ai/documentation/developer/platform-apis/permissions#endpoint-share-with-emails) to share connectors.

Since you used a RBAC token of an API user to deploy the connector, it is technically owned by the API user.

The connector must be shared with you / anyone else before you / them can see it on Tray UI.

To do this, simply execute:

`tray-cdk permissions add [CONNECTOR_NAME] [CONNECTOR_VERSION] --email="[COMMA SEPARATED EMAILS]"`

e.g.

`tray-cdk permissions add my-connector 1.0 --email="john.doe@domain.com, jane.doe@domain.com"`

In the example above, `john.doe@domain.com`, `jane.doe@domain.com` would be two users within the API user's Tray org.
