Yandex Cloud
Search
Discuss with expertTry it for free
  • Customer Stories
  • Documentation
  • Blog
  • All Services
  • System Status
  • Marketplace
    • Featured
    • Infrastructure & Network
    • Data Platform
    • AI for business
    • Security
    • DevOps tools
    • Serverless
    • Monitoring & Resources
  • 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
    • Price calculator
    • Pricing plans
  • Customer Stories
  • Documentation
  • Blog
© 2026 Direct Cursus Technology L.L.C.
Yandex Cloud Functions
  • Comparing with other Yandex Cloud services
    • Overview
      • Overview
      • Top-level function
      • YcFunction interface
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Error handling
    • Using the SDK
  • Tools
  • Pricing policy
  • Access management
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Public materials
  • 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 July 2, 2026
View in Markdown
  • 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 a number and returns it:

fun handle(s: Int): Int = s

Warning

For top-level functions, provide only one parameter.

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

  • In the management console, on the Testing tab of 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 function’s return value.

ExamplesExamples

Processing an HTTP requestProcessing an HTTP request

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

Handler.kt:

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 ?integration=raw 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 returned not as part of the response body, as with the ?integration=raw parameter, but as the HTTP status code.

HTTP request structure outputHTTP request structure output

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

Handler.kt:

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
© 2026 Direct Cursus Technology L.L.C.