Go SDK
The Go SDK includes a zero-dependency client, a schema generator, and a CLI for managing schema files. The recommended flow is to generate a typed client with go generate and then use the generated package for table-safe queries.
The Go SDK includes a zero-dependency client, a schema generator, and a CLI for managing schema files. The recommended flow is to generate a typed client with go generate and then use the generated package for table-safe queries.
Start by creating your database and API keys in the Onyx Cloud Console. Download theonyx-database.json config file and keep your schema available for the code generator.
1) Install the SDK module:
go get github.com/OnyxDevTools/onyx-database-go/onyx@latest
2) Install the Onyx CLI:
# 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 an API key in Onyx Database, then either set environment variables or place an onyx-database.json in your project root.
Option A: Environment variables (preferred)
export ONYX_DATABASE_ID="db_123" export ONYX_DATABASE_BASE_URL="https://api.onyx.dev" export ONYX_DATABASE_API_KEY="key_abc" export ONYX_DATABASE_API_SECRET="secret_xyz"
Option B: Project config file
{
"databaseId": "db_123",
"baseUrl": "https://api.onyx.dev",
"apiKey": "key_abc",
"apiSecret": "secret_xyz"
}Pull the schema and generate Go types and helpers:
onyx schema get onyx init go generate
Use the generated client for typed operations:
package main
import (
"context"
"log"
onyx "your/module/gen/onyx"
)
func main() {
ctx := context.Background()
db, err := onyx.New(ctx, onyx.Config{})
if err != nil {
log.Fatal(err)
}
_, err = db.Users().Save(ctx, onyx.User{
Id: "user_1",
Email: "user@example.com",
Username: "User One",
})
if err != nil {
log.Fatal(err)
}
}If you have any questions or need assistance: