Yandex Cloud
Search
Contact UsGet started
  • Pricing
  • Customer Stories
  • Documentation
  • Blog
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • AI Studio
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Start testing with double trial credits
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Center for Technologies and Society
    • Yandex Cloud Partner program
  • Pricing
  • Customer Stories
  • Documentation
  • Blog
© 2025 Direct Cursus Technology L.L.C.
Tutorials
    • All tutorials
    • URL shortener
    • Ingesting data into storage systems
    • Storing application runtime logs
    • Deploying a web application using the Java Servlet API
    • Developing a Slack bot
    • Developing a Telegram bot
    • Developing a custom integration in API Gateway
    • Developing CRUD APIs for movie services
    • Building a CI/CD pipeline in GitLab
    • Configuring CI/CD between Cloud Functions and SourceCraft
    • Working with an API gateway via WebSocket
    • Building an interactive serverless application using WebSocket
    • Automatically copying objects from one Object Storage bucket to another
    • Visualizing logs in Grafana using the Cloud Logging plugin
    • Canary release of Cloud Functions
    • Interactive debugging of Cloud Functions
    • Creating a Node.js function using TypeScript
    • Running a containerized app in Serverless Containers
    • Streaming Yandex Cloud Postbox events to Data Streams and analyzing them with DataLens
    • Using API Gateway to set up speech synthesis in SpeechKit
    • Connecting to YDB from a Cloud Functions function in Python
    • Connecting to a YDB database from a Cloud Functions function in Node.js
    • API Gateway protection with Smart Web Security
    • Deploying a web app with JWT authorization in API Gateway and authentication in Firebase
    • Automatic data upload to Yandex SpeechSense using Yandex Workflows
    • Configuring responses in Cloud Logging and Yandex Cloud Functions
    • Setting up Workflows integration with Tracker, YandexGPT, and Yandex Cloud Postbox
    • Developing functions in Functions Framework and deploying them to Yandex Serverless Containers
    • Creating a Yandex Cloud Postbox address and checking domain ownership with Terraform
    • Creating an AI agent with Yandex Cloud Functions

In this article:

  • Getting started
  • Required paid resources
  • Create an API gateway
  • Establish a connection
  • Test the connection
  • How to delete the resources you created
  1. Serverless technologies
  2. Working with an API gateway via WebSocket

Working with an API gateway via WebSocket

Written by
Yandex Cloud
Updated at August 14, 2025
  • Getting started
    • Required paid resources
  • Create an API gateway
  • Establish a connection
  • Test the connection
  • How to delete the resources you created

To establish a WebSocket connection to an API gateway:

  1. Create an API gateway.
  2. Establish a connection.
  3. Test the connection.

If you no longer need the resources you created, delete them.

Getting startedGetting started

Sign up for Yandex Cloud and create a billing account:

  1. Navigate to the management console and log in to Yandex Cloud or create a new account.
  2. On the Yandex Cloud Billing page, make sure you have a billing account linked and it has the ACTIVE or TRIAL_ACTIVE status. If you do not have a billing account, create one and link a cloud to it.

If you have an active billing account, you can navigate to the cloud page to create or select a folder for your infrastructure.

Learn more about clouds and folders here.

Required paid resourcesRequired paid resources

The cost of the resources includes the fee for the number of API gateway requests and outbound traffic (see Yandex API Gateway pricing).

Create an API gatewayCreate an API gateway

Management console
  1. In the management console, select the folder where you want to create an API gateway.

  2. In the list of services, select API Gateway.

  3. Click Create API gateway.

  4. In the Name field, enter websocket.

  5. Optionally, in the Description field, provide a description.

  6. In the Specification section, add the specification:

    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}"}'
    
  7. Click Create.

Establish a connectionEstablish a connection

  1. Open the terminal and install wscat:

    npm install -g wscat
    
  2. Connect to the API gateway. Replace <API_gateway_domain>, with the API gateway domain formatted as d5dm1lba80md********.i9******.apigw.yandexcloud.net:

    wscat -c wss://<API_gateway_domain>/connections
    Connected (press CTRL+C to quit)
    
  3. 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:

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

Test the connectionTest the connection

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

CLI

If you do not have the Yandex Cloud CLI installed yet, install and initialize it.

By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also set a different folder for any specific command using the --folder-name or --folder-id parameter.

  1. Retrieve information on the established connection:

    yc serverless api-gateway websocket get <connection_ID>
    
  2. Send a message to the client:

    yc serverless api-gateway websocket send <connection_ID> --data Hello!
    
  3. Terminate the connection:

    yc serverless api-gateway websocket disconnect <connection_ID>
    
  4. Go to the terminal window with the established connection. It should display the following information:

    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.

How to delete the resources you createdHow to delete the resources you created

To stop paying for the resources created, delete the API gateway.

Was the article helpful?

Previous
Configuring CI/CD between Cloud Functions and SourceCraft
Next
Building an interactive serverless application using WebSocket
© 2025 Direct Cursus Technology L.L.C.