Skip to content

VIP-78: First-Class api Declarations (export api NotesApi { ... })

Status: accepted by Will, 2026-11-30. Will: "I accept 76 to 81". (Previously open, 2026-11-28.)

Will's words

"Let's do export api NotesApi { too."

Motivation

Full-stack VibeLang and @effect/ui apps (such as examples/effect-ui-domains) share a typed HTTP contract between server and client using Effect's HttpApi, HttpApiGroup, and HttpApiEndpoint. Even with schema (VIP-15) and schema error (VIP-29), declaring a modest 4-endpoint API still takes ~50 lines of deeply nested builder calls:

export class NotesGroup extends HttpApiGroup.make("notes")
  .add(
    HttpApiEndpoint.get("list", "/notes")
      .setUrlParams(Schema.Struct({ q: Schema.optional(Schema.String) }))
      .addSuccess(Schema.Array(Note))
  )
  .add(
    HttpApiEndpoint.get("get", "/notes/:id")
      .setPath(Schema.Struct({ id: NoteId }))
      .addSuccess(Note)
      .addError(NoteNotFound, { status: 404 })
  )
  .add(
    HttpApiEndpoint.put("save", "/notes/:id")
      .setPath(Schema.Struct({ id: NoteId }))
      .setPayload(Note)
      .addSuccess(Note)
      .addError(NoteSaveError, { status: 422 })
  )
{}
 
export class NotesApi extends HttpApi.make("NotesApi").add(NotesGroup) {}

Furthermore, extracting :id path parameters in pure TypeScript relies on recursive conditional template-literal types that slow down editor responsiveness and produce opaque errors when a path placeholder doesn't match setPath.

Because VibeLang owns the Go compiler, api declarations can parse HTTP route templates directly, validate every path parameter and status code at compile time, and lower straight to standard Effect HttpApi / HttpApiGroup / HttpApiEndpoint with zero runtime wrapper.

Decision

1. Grammar

api is a contextual declaration keyword (following VIP-9 service and VIP-15 schema same-line rules: export? api BindingIdentifier { ... }). Outside declaration starts, api remains an ordinary identifier (const api = client).

ApiDeclaration :
  export? api BindingIdentifier { ApiMember* }

ApiMember :
  base StringLiteral ;?
  group BindingIdentifier { ApiEndpoint* }
  ApiEndpoint

ApiEndpoint :
  HttpMethod StringLiteral ApiEndpointName? ApiInputSpec? -> ApiOutputSpec ;?

HttpMethod :
  GET | POST | PUT | PATCH | DELETE | HEAD | OPTIONS

ApiEndpointName :
  as BindingIdentifier

ApiInputSpec :
  ( ApiParamSection ( , ApiParamSection )* ,? )

ApiParamSection :
  path : SchemaTypeExpr
  query : SchemaTypeExpr
  headers : SchemaTypeExpr
  body : SchemaTypeExpr

ApiOutputSpec :
  SchemaTypeExpr ( @ NumericLiteral )? ( ! ApiErrorEntry ( | ApiErrorEntry )* )?

ApiErrorEntry :
  SchemaTypeExpr ( @ NumericLiteral )?

2. Example

export api NotesApi {
  base "/api"
 
  GET    "/notes"      as list   (query: { q?: string; limit?: int }) -> ReadonlyArray<Note>
  GET    "/notes/:id"  as get    (path: { id: NoteId })               -> Note ! NoteNotFound @ 404
  PUT    "/notes/:id"  as save   (path: { id: NoteId }, body: Note)   -> Note ! NoteSaveError @ 422
  DELETE "/notes/:id"  as remove (path: { id: NoteId })               -> void @ 204 ! NoteNotFound @ 404
}

When as <name> is omitted, the compiler derives a deterministic camelCase endpoint name from the method and static segments (e.g., GET "/health" -> getHealth), or rejects ambiguous unnamed routes with EK7803.

When (path: ...) is omitted for a route containing :id, the compiler automatically synthesizes { readonly id: Schema.String } — or validates exact key equality when (path: { id: NoteId }) is provided.

3. Lowering to Effect HttpApi

export api NotesApi { ... } lowers directly to standard Effect 4 HttpApi classes/values with zero runtime library dependency beyond effect/unstable/httpapi (or @effect/platform):

  • Flat endpoints inside api NotesApi lower into a default group matching the API name ("NotesApi"), while nested group users { ... } blocks lower into named HttpApiGroup.make("users") groups.
  • Every endpoint lowers to HttpApiEndpoint.<method>(name, path) with .setPath(...), .setUrlParams(...), .setHeaders(...), .setPayload(...), .addSuccess(schema, { status }), and .addError(errSchema, { status }).
  • base "/api" lowers to .prefix("/api") on the API.
  • Inline { ... } record types in path, query, headers, body, and return positions use VIP-15's exact schema field lowering (including VIP-77 brand and where).

Because the lowered output is a genuine Effect HttpApi:

  • Typed client: const client = try HttpApiClient.make(NotesApi, { baseUrl: "/api" }) works out of the box, with try client.NotesApi.get({ path: { id } }) returning Note ! (NoteNotFound | HttpClientError).
  • Typed server: HttpApiBuilder.group(NotesApi, "NotesApi", (handlers) => ...) checks every handler's return value and error channel against the api declaration.
  • OpenAPI: OpenApi.fromApi(NotesApi) generates a complete OpenAPI 3.1 document with zero extra annotations.

4. Static Rules

MistakeCode
Route path does not start with / or contains malformed :param segmentEK7801
path: { ... } keys do not match the :param placeholders in the route stringEK7802
Duplicate endpoint name or duplicate (method, normalizedPath) within a groupEK7803
body: declared on a GET or HEAD endpointEK7804
HTTP status code outside 100..599 (or error status outside 400..599)EK7805
Non-schema type used in path, query, headers, body, success, or errorEK7806
Ambient declare api Name { ... }EK7807

5. Declaration Output

.d.ts and .d.vibe.ts emit the standard lowered HttpApi type and value declarations, preserving 100% interoperability with TypeScript callers, HttpApiClient, HttpApiBuilder, and OpenApi.fromApi.

Testing & Verification Plan

  • Parser unit tests + FuzzApiDeclaration verifying api stays a normal identifier outside declaration starts.
  • Differential typechecker tests proving export api produces identical HttpApiClient and HttpApiBuilder types to hand-written HttpApi.make(...).
  • Compiler acceptance cases (typescript/vibelang/cases/vip78-*) covering valid client+server round-trip, path-param mismatch (EK7802), GET with body (EK7804), and status-code checks (EK7805).
  • End-to-end port of examples/effect-ui-domains shared HTTP API to export api DomainsApi { ... }.

Open Questions for Will

  1. Should base "/api" be an inner clause (base "/api") or a header modifier (export api NotesApi at "/api" { ... })?
  2. Should group blocks also support per-group middleware declarations (e.g., middleware AuthMiddleware), or keep middleware wired at the HttpApi composition site?