Simple backend

A backend for DivKit can be deployed using the Yandex Cloud 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.

First, you need to create the function itself:

  1. In the management console, 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:
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!"
      }
    }
  ]
}
"""
  1. 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.
  2. 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:
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")
}
  1. Create a handler file src/main/kotlin/cloud/divkit/Handler.kt similar to the one created in the simple function section.
  2. Modify the handle(request: Request) method as follows:
fun handle(request: Request): Response {
    val card = divan {
        data(
            logId = "greeting",
            div = text("Hello, Username!")
        )
    }
    return Response(200, card.toJson())
}
  1. Package these files into a zip-archive.

The complete example is available at this link.

Now you can create a function based on it:

  1. Create a new Cloud function the same way as in the previous section.
  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.

Getting Started with Yandex Cloud
Guide to Creating Kotlin Functions
Simple Cloud Function with DivKit Layout
Cloud Function Using Kotlin DSL

Learn more

Follow DivKit news in the Telegram channel: http://t.me/divkit_news

You can also discuss topics of interest in the DivKit user community in Telegram: https://t.me/divkit_community_ru

DivKit Repository