---
metadata:
  - name: generator
    content: Diplodoc Platform v5.50.3
alternate:
  - https://divkit.tech/docs/en/quickstart/simple-backend.md
  - https://divkit.tech/docs/ru/quickstart/simple-backend.md
---
> **Documentation Index:** Fetch the complete configuration index at https://divkit.tech/docs/en/llms.txt

# Simple backend
A backend for DivKit can be deployed using the [Yandex Cloud Functions](https://yandex.cloud/ru/services/functions) service. This requires a Yandex Cloud account.

## Creating a function in Kotlin
Getting started with Yandex Cloud is described in detail in the [instructions](https://yandex.cloud/ru/docs/getting-started).

First, you need to create the function itself:
1. In [the management console](https://yandex.cloud/ru/docs/getting-started), go to the directory where you want to create the function.
2. Select the Cloud Functions service.
3. Click the **Create function** button.
4. Enter a function name.
5. Click the **Create** button.
6. Select the **Kotlin** runtime environment.
7. Click the **Continue** button.

The function editor will appear on the screen where you need to do the following:
1. Select the **Code editor** option for defining the function.
2. Click the **Create file** button.
3. Name it `Handler.kt`.
4. To the right of the file list, paste this code:
```Kotlin translate=no
package cloud.divkit

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, card)
}

private const val card = """
{
  "log_id": "cloud_divkit",
  "states": [
    {
      "state_id": 0,
      "div": {
        "type": "text",
        "text": "Hello, Username!"
      }
    }
  ]
}
"""
```
6. Specify the entry point `cloud.divkit.Handler::handle`. Entry point consists of the package name, handler file name, and handler method. Additional information can be found in [this guide](https://yandex.cloud/ru/docs/functions/lang/kotlin/handler).
7. Click the **Save changes** button.

## Using Kotlin DSL
Kotlin DSL provides a way to describe DivKit layout directly in the Kotlin application code. To use it, you need to connect an external dependency.
Build a small project:
1. Create a `build.gradle.kts` file.
2. Specify the following code in it:
```Kotlin translate=no
plugins {
    kotlin("jvm") version "2.0.0"
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("com.fasterxml.jackson.core:jackson-annotations:2.18.3")
    implementation("com.fasterxml.jackson.core:jackson-databind:2.18.3")
    implementation("com.yandex.div:kotlin-json-builder:31.0.0")
}
```
3. Create a handler file `src/main/kotlin/cloud/divkit/Handler.kt` similar to the one created in the [simple function section](#creating-a-function-in-kotlin).
4. Modify the `handle(request: Request)` method as follows:
```Kotlin translate=no
fun handle(request: Request): Response {
    val card = divan {
        data(
            logId = "greeting",
            div = text("Hello, Username!")
        )
    }
    return Response(200, card.toJson())
}
```
5. Package these files into a zip-archive.

The complete example is available at [this link](https://github.com/divkit/divkit/tree/main/samples/cloud-functions/kotlin-json-builder).

Now you can create a function based on it:
1. Create a new Cloud function the same way as in the [previous section](#creating-a-function-in-kotlin).
2. In the editor, select the **ZIP archive** option.
3. Click the **Attach file** button.
4. Specify the path to the created zip-archive.
5. Set the entry point to `cloud.divkit.Handler::handle`.
6. Click the **Save changes** button.

## Displaying the Result
You can check the response of the created function in the DivKit demo application:
1. Select the desired function from the list.
2. Copy the link specified in the **Link to invoke** section.
3. Open the demo application.
4. Go to **Demo** (Android) or **Playground** (iOS) section.
5. Paste the link.
6. Click the **Show the result** button.

It's worth noting that by default all functions are private, and a corresponding token is required to access them. For the demo application to be able to display the layout, you need to make the function public.

## Useful Links
[Getting Started with Yandex Cloud](https://yandex.cloud/ru/docs/getting-started)
[Guide to Creating Kotlin Functions](https://yandex.cloud/ru/docs/functions/quickstart/create-function/kotlin-function-quickstart)
[Simple Cloud Function with DivKit Layout](https://github.com/divkit/divkit/tree/main/samples/cloud-functions/simple/Handler.kt)
[Cloud Function Using Kotlin DSL](https://github.com/divkit/divkit/tree/main/samples/cloud-functions/kotlin-json-builder)

<!-- source: en/_includes/index/troubleshooting.md -->
## Learn more {#troubleshooting}

You can discuss topics of interest in the DivKit user community in Telegram: [https://t.me/divkit_community_en](https://t.me/divkit_community_en).



[DivKit Repository](https://github.com/divkit/divkit)
<!-- endsource: en/_includes/index/troubleshooting.md -->
