Skip to content

Go backend

A Go HTTP server built on the standard library. main.go loads the environment, sets up the client and serves a health check. vaif/client.go creates the Go client from the project ID and keys in the environment.

Terminal window
vaif init --template go-backend-api

Run it in a new, empty folder. It writes vaif.config.json and the template's files there. It stops if the folder already has a vaif.config.json, and leaves any other file that already exists alone; --force overwrites both.

It asks which features to include. This template has files for Database, Authentication, Storage and Functions; Database and Authentication are selected to start with. To skip the question, name them: vaif init --template go-backend-api --features database,auth,storage,functions. Add one later with vaif init --template go-backend-api --add-features <feature>.

FileWritten
main.goAlways
vaif/client.goAlways
go.modAlways
.env.exampleAlways
.gitignoreAlways
README.mdAlways
migrations/0001_initial.sqlWith Database
models/models.goWith Database
middleware/auth.goWith Authentication or Storage
handlers/storage.goWith Storage
functions/hello.goWith Functions

Authentication adds a middleware that checks the caller’s access token. Storage adds file handlers, and Database adds models for the starter tables.

The code this template writes uses the client for its language:

The server builds only once the Go client can be installed. Until then, call the REST API from Go with net/http.

Copy .env.example to .env and fill it in. The project ID and the keys are on Security > Credentials in the console.

VariableValue
VAIF_PROJECT_IDYour project ID.
VAIF_PUBLISHABLE_KEYThe project's publishable key (vk_pub_). Row rules apply to it, so it is safe in an app.
VAIF_SECRET_KEYThe project's secret key (vk_srv_), for server code only. It bypasses row rules; never ship it in a browser or mobile app.

The template writes one SQL file for your database. The comment line above each CREATE TABLE sets that table's row rules.

migrations/0001_initial.sql, written with the Database feature, is an example: edit it to describe your own tables before you apply it.

migrations/0001_initial.sql
-- Initial migration. Customize this schema for your project; vaif db push
-- applies it. The comment directly above each CREATE TABLE sets that table's
-- row rules: here each signed-in user reads and changes only their own rows,
-- and the owner column is filled in from their sign-in on insert. Keep a rule
-- comment on every table you add: a table with no row rules is closed to
-- signed-out visitors but open to every signed-in user.
-- vaif:owner id
CREATE TABLE IF NOT EXISTS "users" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
"email" text NOT NULL UNIQUE,
"name" text,
"created_at" timestamp with time zone NOT NULL DEFAULT now()
);
-- vaif:owner user_id
CREATE TABLE IF NOT EXISTS "posts" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
"user_id" uuid NOT NULL REFERENCES "users"("id"),
"title" text NOT NULL,
"content" text,
"published" boolean NOT NULL DEFAULT false,
"created_at" timestamp with time zone NOT NULL DEFAULT now()
);
  • users (-- vaif:owner id): each signed-in user reads and changes only their own rows, and id is filled in from their sign-in.
  • posts (-- vaif:owner user_id): each signed-in user reads and changes only their own rows, and user_id is filled in from their sign-in.

Put your project ID in vaif.config.json ("projectId"), sign in, and apply it. With no --dir, vaif db push finds the template's SQL folder, or a ./migrations folder, and applies its .sql files in name order.

Terminal window
vaif login
vaif db push --dry-run # what each file would change
vaif db push
Terminal window
go mod tidy
go run main.go