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
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
  • Blog
  • Pricing
  • Documentation
Yandex project
© 2025 Yandex.Cloud LLC
Yandex Cloud Functions
  • Comparison with other Yandex Cloud services
    • Overview
      • Overview
      • Top-level function
      • YcFunction interface
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Tools
  • Pricing policy
  • Access management
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Release notes
  • FAQ

In this article:

  • Examples
  • Processing an HTTP request
  • HTTP request structure output
  1. Developing in Kotlin
  2. Programming model
  3. Top-level function

Using a top-level function for a handler in Kotlin

Written by
Yandex Cloud
Updated at November 2, 2024
  • Examples
    • Processing an HTTP request
    • HTTP request structure output

To create a handler in Kotlin, you can set a top-level function. This is a function declared within a package and requiring no class for its creation.

Here is an example of a handler that accepts and returns a number:

fun handle(s: Int): Int = s

Warning

Use only one parameter to provide in top-level functions.

You can test the function from the above example in the following ways:

  • In the management console, in the Testing tab on the function page.

  • Using the following HTTPS request with the ?integration=raw parameter:

    curl \
         --header "Authorization: Bearer <IAM_token>" \
         --data "<number>" \
         "https://functions.yandexcloud.net/<function_ID>?integration=raw"
    

    In the --data parameter, specify the number to be returned by the function.

ExamplesExamples

Processing an HTTP requestProcessing an HTTP request

The script below processes an incoming HTTP request and outputs the results: HTTP response code and response body.

Handler.kt file:

data class Request(
    val httpMethod: String?,
    val headers: Map<String, String> = mapOf(),
    val body: String = ""
)

data class Response(
    val statusCode: Int,
    val body: String
)

fun handle(request: Request): Response {
    return Response(200, "Hello World!")
}

The result format depends on whether the user provided the ?integration=raw parameter in the request:

  • If ?integration=raw was provided:

    {
        "statusCode": 200,
        "body": "Hello World!"
    }
    
  • If ?integration=raw was not provided:

    "Hello World!"
    

    The 200 code will be delivered not as part of the response body, as in the case with the ?integration=raw parameter, but as an HTTP response code.

HTTP request structure outputHTTP request structure output

The script below processes an incoming HTTP request and outputs its structure and HTTP response code.

Handler.kt file:

data class Response(
    val statusCode: Int,
    val body: String
)

fun handle(request: String): Response {
    return Response(200, request)
}

Was the article helpful?

Previous
Overview
Next
YcFunction interface
Yandex project
© 2025 Yandex.Cloud LLC