Kotlin SDK Quickstart

Connect to your Onyx Cloud Database with generated Kotlin models and a typed client. Install the dependency, set credentials, generate code with the Onyx CLI, then save and query with IDE autocomplete.

Looking for other supported sdks?

Overview

Target a cloud database with typed models and keep your schema in sync via the Onyx CLI.

Install

Add the dependency to your build:

repositories {
    mavenCentral()
}

dependencies {
    implementation("dev.onyx:onyx-database:3.8.6")
}

Install the Onyx CLI for schema pulls and codegen:

# Homebrew (macOS)
brew tap OnyxDevTools/onyx-cli
brew install onyx-cli

# Or install directly
curl -fsSL https://raw.githubusercontent.com/OnyxDevTools/onyx-cli/main/scripts/install.sh | bash

onyx version

Create a database and publish a schema

Login to cloud.onyx.dev to create a database and an API key & secret.

Here is a minimal example schema:

{
  "tables": [
    {
      "name": "User",
      "identifier": {"name": "id", "generator": "UUID", "type": "String"},
      "type": "DEFAULT",
      "attributes": [
        {"name": "id","type": "String","maxSize": null,"isNullable": false},
        {"name": "status","type": "String","maxSize": null,"isNullable": false},
        {"name": "email", ...},
        {"name": "createdAt", ...}
      ]
    }
  ]
}

Configure credentials

Set credentials using environment variables or a project config file.

Option A: Environment variables

export ONYX_DATABASE_ID="your-database-id"
export ONYX_DATABASE_API_KEY="key_abc"
export ONYX_DATABASE_API_SECRET="secret_xyz"

Option B: Project config file (onyx-database.json)

{
  "databaseId": "db_123",
  "baseUrl": "https://api.onyx.dev",
  "apiKey": "key_abc",
  "apiSecret": "secret_xyz"
}

Generate Kotlin models

Pull the schema and generate Kotlin models + table helpers:

onyx schema get
onyx gen --lang kotlin --out ./generated/onyx

Save & query

Persist and query entities with the Kotlin SDK:

fun main() {
    val db = onyx.init<Any>() // resolves credentials from env/config

    db.save(User(
        id = "user_123",
        name = "John Doe",
        email = "john@example.com"
    ))

    val users = db
        .from<User>()
        .where("email" eq "john@example.com")
        .list<User>()

    println(users)
}

What else can I do?

Resources

Next Steps

Need Help?

If you have any questions or need assistance: