[Yandex Cloud documentation](../../index.md) > [Tutorials](../index.md) > [Serverless technologies](index.md) > Serverless-based backend > Working with an API gateway via WebSocket

# Working with an API gateway via WebSocket


To establish a WebSocket connection to an API gateway:

1. [Create an API gateway](#create).
1. [Establish a connection](#connect).
1. [Test the connection](#check).

If you no longer need the resources you created, [delete](#clear-out) them.

## Getting started {#before-you-begin}

Sign up for Yandex Cloud and create a [billing account](../../billing/concepts/billing-account.md):
1. Navigate to the [management console](https://console.yandex.cloud) and log in to Yandex Cloud or create a new account.
1. On the **[Yandex Cloud Billing](https://center.yandex.cloud/billing/accounts)** page, make sure you have a billing account linked and it has the `ACTIVE` or `TRIAL_ACTIVE` [status](../../billing/concepts/billing-account-statuses.md). If you do not have a billing account, [create one](../../billing/quickstart/index.md) and [link](../../billing/operations/pin-cloud.md) a cloud to it.

If you have an active billing account, you can create or select a [folder](../../resource-manager/concepts/resources-hierarchy.md#folder) for your infrastructure on the [cloud page](https://console.yandex.cloud/cloud).

[Learn more about clouds and folders here](../../resource-manager/concepts/resources-hierarchy.md).

### Required paid resources {#paid-resources}

The cost of resources includes the fee for the number of API gateway requests and outgoing traffic (see [Yandex API Gateway pricing](../../api-gateway/pricing.md)).

## Create an API gateway {#create}

{% list tabs group=instructions %}

- Management console {#console}

    1. In the [management console](https://console.yandex.cloud), select the folder where you want to create an API gateway.
    1. Navigate to **API Gateway**.
    1. Click **Create API gateway**.
    1. In the **Name** field, enter `websocket`.
    1. Optionally, in the **Description** field, provide a description.
    1. In the **Specification** section, add the specification:

        ```yaml
        openapi: 3.0.0
        info:
          title: Test API
          version: 1.0.0
        paths:
          /connections:
            x-yc-apigateway-websocket-message:
              summary: Get connection identifier
              operationId: getConnectionID
              parameters:
                - name: X-Yc-Apigateway-Websocket-Connection-Id
                  in: header
                  description: Websocket connection identifier
                  required: true
                  schema:
                    type: string
              responses:
                '200':
                  description: Connection identifier
                  content:
                    text/plain:
                      schema:
                        type: string
              x-yc-apigateway-integration:
                type: dummy
                http_code: 200
                http_headers:
                  Content-Type: application/json
                content:
                  text/plain: '{"connection_id":"{X-Yc-Apigateway-Websocket-Connection-Id}"}'
        ```

    1. Click **Create**.

{% endlist %}

## Establish a connection {#connect}

1. Open the terminal and install [wscat](https://www.npmjs.com/package/wscat):

    ```bash
    npm install -g wscat
    ```

1. Connect to the API gateway. Replace `<API_gateway_domain>`, with the API gateway domain formatted as `d5dm1lba80md********.i9******.apigw.yandexcloud.net`:

    ```bash
    wscat -c wss://<API_gateway_domain>/connections
    Connected (press CTRL+C to quit)
    ```

1. Type a message and press `Enter`.

The message will go to the API gateway over the established connection. The API gateway will call integration and return a response that you will see on your screen. The response will contain the established connection ID:

```bash
> Hello!
< {"connection_id":"<connection_ID>"}
```

## Test the connection {#check}

Open a new terminal window and test the connection using the connection ID you got.

{% list tabs group=instructions %}

- CLI {#cli}

    If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

    The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

    1. Retrieve information on the established connection:

        ```bash
        yc serverless api-gateway websocket get <connection_ID>
        ```

    1. Send a message to the client:

        ```bash
        yc serverless api-gateway websocket send <connection_ID> --data Hello!
        ```

    1. Terminate the connection:

        ```bash
        yc serverless api-gateway websocket disconnect <connection_ID>
        ```

    1. Go to the terminal window with the established connection. It should display the following information:

        ```bash
        wscat -c wss://<API_gateway_domain>/connections
        Connected (press CTRL+C to quit)
        > Hello!
        < {"connection_id":"<connection_ID>"}
        < Hello!
        Disconnected (code: 1000, reason: "")
        ```

        Where `API_gateway_domain` is a string formatted as `d5dm1lba80md********.i9******.apigw.yandexcloud.net`.

{% endlist %}

## How to delete the resources you created {#clear-out}

To stop paying for the resources you created, delete the [API gateway](../../api-gateway/operations/api-gw-delete.md).