Yandex Cloud
  • Services
  • Solutions
  • Why Yandex Cloud
  • Pricing
  • Documentation
  • Contact us
Get started
Language / Region
© 2022 Yandex.Cloud LLC
Yandex API Gateway
  • Getting started
  • Step-by-step instructions
    • All instructions
    • Creating API gateways
    • Updating API gateways and their specifications
    • Connecting a domain
    • Deleting API gateways
    • Monitoring
    • Viewing the execution log
  • Practical guidelines
    • Deploying a web application
    • Developing a skill for Alice and a website with authorization
    • Developing a Slack bot
    • Developing a Telegram bot
  • Concepts
    • Relationship between service resources
    • Specification extensions
      • Overview
      • Static response
      • Invoking a function
      • Integration with Serverless Containers
      • Access via HTTP
      • Integration with Object Storage
      • DataSphere integration
      • Data Streams integration
      • Message Queue integration
      • Greedy parameters
      • Generalized HTTP method
      • Authorization using a function
    • Quotas and limits
  • Access management
  • Pricing policy
  • API reference
    • Authentication in the API
    • gRPC
      • Overview
      • ApiGatewayService
      • OperationService
    • REST
      • Overview
      • ApiGateway
        • Overview
        • addDomain
        • create
        • delete
        • get
        • getOpenapiSpec
        • list
        • listAccessBindings
        • listOperations
        • removeDomain
        • setAccessBindings
        • update
        • updateAccessBindings
  • Questions and answers
  1. Getting started

Getting started with API Gateway

Written by
Yandex Cloud
  • Before you start
  • Create an API gateway
  • Add an integration with the function
    • Create a function
    • Extend the API gateway specification
    • Access the function via the API gateway

Using these instructions, you'll create and test different types of extensions: first, you'll set up an API gateway for getting static responses and then add integration for invoking functions.

Before you start

To get started in Yandex Cloud:

  1. Log in to management console. If you aren't registered, go to the management console and follow the instructions.
  2. On the billing page make sure that you enabled a billing account and that it has the ACTIVE or TRIAL_ACTIVE status. If you don't have a billing account, create one.
  3. If you don't have a folder, create one.

Create an API gateway

Create an API gateway and access it.

Management console

To create an API gateway:

  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 numbers.

  5. (optional) In the Description field, enter a description.

  6. In the Specification section, add a specification:

    openapi: "3.0.0"
    info:
      version: 1.0.0
      title: Test API
    paths:
      /hello:
        get:
          summary: Say hello
          operationId: hello
          parameters:
            - name: user
              in: query
              description: User name to appear in greetings
              required: false
              schema:
                type: string
                default: 'world'
          responses:
            '200':
              description: Greeting
              content:
                'text/plain':
                   schema:
                     type: "string"
          x-yc-apigateway-integration:
            type: dummy
            http_code: 200
            http_headers:
              'Content-Type': "text/plain"
            content:
              'text/plain': "Hello, {user}!\n"
    
  7. Click Create.

  8. Access the API gateway.

    1. In the window that opens, click on the created API gateway.

    2. Copy the Domain field value and create a link like: https://<domain>/hello?user=API. The resulting link should look like this:

      https://falrnjna8r5vj88ero6a.apigw.yandexcloud.net/hello?user=API
      
    3. Access the API gateway using one of the commands:

      curl https://falrnjna8r5vj88ero6a.apigw.yandexcloud.net/hello?user=API
      curl https://falrnjna8r5vj88ero6a.apigw.yandexcloud.net/hello
      

Add an integration with the function

Create a function

Create a function to get a list of numbers. Read more about functions in the documentation for Cloud Functions.

Management console

To create a function:

  1. Create a function:
    1. In management console, select the folder where you want to create your function.
    2. Click Create resource.
    3. Choose Function.
    4. In the Name field, specify list.
    5. Click Create.
    6. Make the function public.
  2. Create the function version:
    1. In the window that opens, select the function you created.

    2. Under Latest version, click Create in editor.

    3. In the window that opens, in the Runtime environment field, choose nodejs12.

    4. In the Method field, select the code editor.

    5. Click Create file in the editor below.

      1. In the window that opens, enter the index.js file name.
      2. Click Create.
    6. Paste the following code in the index.js file:

      module.exports.handler = async (event) => {
          return {
              "statusCode": 200,
              "headers": {"content-type": "application/json"},
              "body": "[0, 1, 2]"
          };
      };
      
    7. In the Entry point field, enter index.handler.

    8. Click Create version.

Extend the API gateway specification

Add function information to the API gateway specification.

Management console

To update an API gateway specification:

  1. In the management console, select the folder to update the API gateway in.

  2. In the window that opens, select the API gateway and click .

  3. In the menu that opens, click Edit.

  4. Under Specification, add an extended version of the specification.

    Added the /numbers method that uses the x-yc-apigateway-integration extension of the cloud-functions type to invoke a function by ID.

    To ensure that the API gateway works properly, in the function_id parameter, specify the ID of the function to invoke.

    To let the API gateway access a private function, in the parameter service_account_id specify a service account that has the rights to invoke the function.

    openapi: "3.0.0"
    info:
      version: 1.0.0
      title: Test API
    paths:
      /hello:
        get:
          summary: Say hello
          operationId: hello
          parameters:
            - name: user
              in: query
              description: User name to appear in greetings
              required: false
              schema:
                type: string
                default: 'world'
          responses:
            '200':
              description: Greeting
              content:
                'text/plain':
                   schema:
                     type: "string"
          x-yc-apigateway-integration:
            type: dummy
            http_code: 200
            http_headers:
              'Content-Type': "text/plain"
            content:
              'text/plain': "Hello, {user}!\n"
      /numbers:
        get:
          summary: List some numbers
          operationId: listNumbers
          responses:
            '200':
              description: Another example
              content:
                'application/json':
                   schema:
                     type: "array"
                     items:
                       type: "integer"
          x-yc-apigateway-integration:
            type: cloud_functions
            function_id: <function ID>
            service_account_id: <service account ID>
    

Access the function via the API gateway

Note

To let the API gateway access the function, make it public or add to the specification a service account that has the rights to invoke the function.

Access the API gateway:

curl https://falrnjna8r5vj88ero6a.apigw.yandexcloud.net/numbers
[0, 1, 2]

See also

  • Concepts for using the service.
  • Step-by-step instructions for managing API gateways.

Was the article helpful?

Language / Region
© 2022 Yandex.Cloud LLC
In this article:
  • Before you start
  • Create an API gateway
  • Add an integration with the function
  • Create a function
  • Extend the API gateway specification
  • Access the function via the API gateway