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.
Install the SDK and CLI:
go install github.com/OnyxDevTools/onyx-database-go/onyx@latest go install github.com/OnyxDevTools/onyx-database-go/cmd/onyx-go@latest
Initialize the go:generate scaffold:
onyx-go gen init
The default layout matches the README:
. ├── generate.go ├── api/onyx.schema.json ├── config/onyx-database.json └── gen/onyx/
Then generate the client:
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)
}
}Credentials can be resolved from env vars:
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"
Or pass explicit config:
db, err := onyx.New(ctx, onyx.Config{
DatabaseID: "db_123",
DatabaseBaseURL: "https://api.onyx.dev",
APIKey: os.Getenv("ONYX_DATABASE_API_KEY"),
APISecret: os.Getenv("ONYX_DATABASE_API_SECRET"),
CacheTTL: 10 * time.Minute,
LogRequests: true,
})Fetch, validate, or publish schemas using the CLI:
onyx-go schema info onyx-go schema get onyx-go schema validate --schema ./api/onyx.schema.json onyx-go schema publish --schema ./api/onyx.schema.json
If you have any questions or need assistance: