Getting started with 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:
- Log in to management console. If you aren't registered, go to the management console and follow the instructions.
- On the billing page make sure that you enabled a billing account and that it has the
ACTIVE
orTRIAL_ACTIVE
status. If you don't have a billing account, create one. - If you don't have a folder, create one.
Create an API gateway
Create an API gateway and access it.
To create an API gateway:
-
In the management console, select the folder where you want to create an API gateway.
-
In the list of services, select API Gateway.
-
Click Create API gateway.
-
In the Name field, enter
numbers
. -
(optional) In the Description field, enter a description.
-
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"
-
Click Create.
-
Access the API gateway.
-
In the window that opens, click on the created API gateway.
-
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
-
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.
To create a function:
- Create a function:
- In management console, select the folder where you want to create your function.
- Click Create resource.
- Choose Function.
- In the Name field, specify
list
. - Click Create.
- Make the function public.
- Create the function version:
-
In the window that opens, select the function you created.
-
Under Latest version, click Create in editor.
-
In the window that opens, in the Runtime environment field, choose
nodejs12
. -
In the Method field, select the code editor.
-
Click Create file in the editor below.
- In the window that opens, enter the
index.js
file name. - Click Create.
- In the window that opens, enter the
-
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]" }; };
-
In the Entry point field, enter
index.handler
. -
Click Create version.
-
Extend the API gateway specification
Add function information to the API gateway specification.
To update an API gateway specification:
-
In the management console, select the folder to update the API gateway in.
-
In the window that opens, select the API gateway and click .
-
In the menu that opens, click Edit.
-
Under Specification, add an extended version of the specification.
Added the
/numbers
method that uses thex-yc-apigateway-integration
extension of thecloud-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]