Golem add http endpoint go

From golemcloud/golem · View interactive version

Golem add http endpoint go is an AI agent skill from the GitHub repository golemcloud/golem. Exposing a Go agent's methods over HTTP. Use when the user wants to add an HTTP endpoint/route to an agent, mount an agent under a URL path, make agent methods callable over HTTP, or map path/query/header parameters in a Go Golem project.

How to use this skill

Skill file

Exposing a Go Agent over HTTP

Overview

HTTP mounting is metadata only: you declare, on the agent's definition, a URL prefix for the agent and a route for each method. The platform routes matching requests to the right instance and method — there is no incoming-request handler to write. To handle raw requests yourself (a net/http handler, streaming bodies, a website), use an HTTP router instead (see golem-http-router-go).

Two pieces, both on the definition:

  1. Spec.HTTP = &golem.Mount{Path: "…"} — the agent-level prefix. On a key mount, its {var} segments bind the agent's user-supplied constructor (ID) fields, so a request URL selects the instance. Every user-supplied ID field must appear as a {var} on a key mount; exclude auto-injected golem.Principal fields. An ID mount instead binds only the required agent ID.
  2. golem.HTTP(golem.GET("/suffix"), …) as a method option — one or more routes per method, with request data bound to the method's input fields.

Steps

  1. Add a mount to the agent's Spec: HTTP: &golem.Mount{Path: "/counters/{name}"}.
  2. Add routes to methods with the golem.HTTP(...) option and a verb constructor.
  3. Bind request data to input fields (path {var}, golem.Query, golem.Header, or the JSON body).
  4. Build, then deploy behind an HTTP API so the routes are served.

Example (definition)

package counter

import "github.com/golemcloud/golem/sdks/go/golem"

type ID struct{ Name string } // bound by {name} in the mount path

type AddIn struct{ By int64 }

var Agent = golem.DefineAgent[ID](golem.Spec{
	Name:        "CounterAgent",
	Description: "A counter exposed over HTTP",
	// {name} binds ID.Name; CORS/Auth optional.
	HTTP: &golem.Mount{Path: "/counters/{name}", CORS: []string{"*"}},
})

var (
	// GET /counters/{name}/value  — options compose: Desc + HTTP in one call.
	Value = Agent.Method[golem.Unit, int64]("value", golem.Description("Read the current value"), golem.HTTP(golem.GET("/value")))

	// POST /counters/{name}/add  — AddIn arrives as the JSON body
	Add = Agent.Method[AddIn, int64]("add", golem.Description("Add to the count"), golem.HTTP(golem.POST("/add")))
)

The handlers are ordinary handlers (see golem-add-agent-go) — nothing HTTP-specific in the impl.

Required Agent ID Mounts and Creation

Use an ID mount to address an existing durable, non-router agent without constructor inputs in the URL:

HTTP: &golem.Mount{
    Path:        "/tasks/{instance}",
    AgentID:     golem.AgentIDPath("instance"),
    CreateRoute: "/tasks/create",
},

For query addressing, use Path: "/tasks" and AgentID: golem.AgentIDQuery("instance"). The ID is required; there is no optional binding or keyed fallback. ID mounts cannot bind constructor fields or combine with PhantomAgent or ephemeral mode. The query ID name is reserved from explicit and implicit method query bindings and protocol controls. Keep fresh-per-request PhantomAgent and ephemeral lifecycle policies on key mounts.

CreateRoute declares a full exact POST path, not a mount suffix, and requires AgentID. Only literals and system variables are allowed in this path. Send a JSON object with one field per user-supplied constructor parameter, as an endpoint body has one field per method parameter, for example {"name": "first"}; an optional parameter may be left out. Other fields, such as env, config or keyed, are rejected with 400. The new agent has no key. Route authentication does not grant configuration authority. Use authorized operator REST creation or SDK creation for overrides. Creation inherits mount authentication/CORS, waits for the constructor, and returns 201 Created with agentId/agentFingerprint JSON and an ID-mounted Location. Current HTTP callers have no caller scope, even with authentication, so Idempotency-Key is ignored; retries create new unkeyed agents.

ID selection never creates an agent. File serving supports both key and ID mounts but is lookup-only: missing keys and unknown, deleted, wrong-type or wrong-environment IDs return terminal 404, without creation or handler fallback. Missing files on an existing target retain normal mapping fallback.

Binding request data to input fields

```go

Lookup = Agent.MethodLookupIn, ItemInfo)))

```

Auth & CORS

```go

golem.HTTP(golem.GET("/items/{sku}", golem.EndpointAuth(true)))

```

Verbs

golem.GET, golem.POST, golem.PUT, golem.DELETE, and golem.Custom("VERB", "/path"). GET/HEAD are bodyless — bind all user-supplied inputs from path/query/header, excluding auto-injected golem.Principal.

Durable Stream Route Customization

A method that takes or returns golem.AgentStreams is served as a Durable Streams URL family. golem.DurableStreams customizes it:

type UploadIn struct {
    Chunks golem.AgentStream[byte]
    File   string
}

Upload = Agent.Method[UploadIn, int64]("upload",
    golem.HTTP(golem.POST("/upload?file={file}", golem.DurableStreams(golem.StreamRoute{
        Slots: []golem.StreamSlot{
            {Input: "chunks", Name: "data", ContentType: "application/vnd.example.media"},
            {Result: true, Name: "size"},
        },
        MaxReadersPerStream: 8,
    }))))

Serving the Agent's Own Files

Mount.ExposeFiles serves files from the agent's filesystem under its mount, without calling a method:

var Agent = golem.DefineAgent[ID](golem.Spec{
    Name: "MediaAgent",
    HTTP: &golem.Mount{
        Path: "/media/{name}",
        ExposeFiles: []golem.FileMapping{
            {Route: "/latest", Path: "/latest.txt"},       // one file
            {Route: "/files/*", Path: "/public/$1"},       // a directory tree
        },
    },
})

Key Constraints

Related Skills

| Skill | When to Load |

|-------|--------------|

| golem-add-agent-go | Define the agent whose methods you're exposing |

| golem-make-http-request-go | Make an outgoing HTTP request from agent code |

| golem-multi-instance-agent-go | The {var} path segments select an instance by ID |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.