Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Start testing with double trial credits
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
    • Yandex Cloud Partner program
  • Blog
  • Pricing
  • Documentation
© 2025 Direct Cursus Technology L.L.C.
Yandex API Gateway
  • Getting started
    • All tutorials
    • Deploying a web application using the Java Servlet API
    • Creating a skill for Alice and a website with authorization
    • Developing a Slack bot
    • Developing a Telegram bot
    • Developing a Telegram bot for text and audio recognition
    • Developing a custom integration
    • Developing CRUD APIs for movie services
    • Working with an API gateway via WebSocket
    • Creating an interactive serverless application using WebSocket
    • Building a CI/CD pipeline in GitLab with serverless products
    • Interactive debugging of Cloud Functions functions
    • Deploying a web app with JWT authorization in API Gateway and authentication in Firebase
    • Canary release of a Cloud Functions function
    • Using API Gateway to set up speech synthesis in Yandex SpeechKit
    • Running a containerized app in Yandex Serverless Containers
    • URL shortener
    • Entering data into storage systems
    • API Gateway protection with Smart Web Security
  • Access management
  • Pricing policy
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Release notes
  • FAQ

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. Tutorials
  2. Working with an API gateway via WebSocket

Working with an API gateway via WebSocket

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

To connect to an API gateway via the WebSocket protocol:

  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 in Yandex Cloud and create a billing account:

  1. Navigate to the management console and log in to Yandex Cloud or register 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 to operate in.

Learn more about clouds and folders.

Required paid resourcesRequired paid resources

The cost of the resources includes charges for the number of API gateway requests and outgoing 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. (Optional) In the Description field, enter 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 the wscat utility:

    npm install -g wscat
    
  2. Connect to the API gateway. Instead of <API_gateway_domain>, specify 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 be sent to the API gateway via the existing connection. The API gateway will call integration and send a response that you will see on your screen. The response will contain the connection ID:

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

Test the connectionTest the connection

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

CLI

If you do not have the Yandex Cloud (CLI) command line interface yet, install and initialize it.

The folder specified when creating the CLI profile is used by default. To change the default folder, use the yc config set folder-id <folder_ID> command. You can specify a different folder using the --folder-name or --folder-id parameter.

  1. Retrieve information on the existing 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
Developing CRUD APIs for movie services
Next
Creating an interactive serverless application using WebSocket
© 2025 Direct Cursus Technology L.L.C.