Developers

Call Claude, GPT and Gemini from Go

Go needs no SDK: a plain JSON POST to the Somnus chat completions endpoint with net/http is enough.

Install

bash
(no install — standard library)

Basic request

go
package main

import (
	"bytes"
	"fmt"
	"io"
	"net/http"
	"os"
)

func main() {
	body := []byte(`{
	  "model": "claude-sonnet-5",
	  "messages": [
	    {"role": "user", "content": "Explain recursion in one sentence."}
	  ]
	}`)
	req, _ := http.NewRequest("POST", "https://gateway-production-c837.up.railway.app/v1/chat/completions", bytes.NewReader(body))
	req.Header.Set("Authorization", "Bearer "+os.Getenv("SOMNUS_API_KEY"))
	req.Header.Set("Content-Type", "application/json")
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer res.Body.Close()
	out, _ := io.ReadAll(res.Body)
	fmt.Println(string(out))
}

Streaming

go
package main

import (
	"bytes"
	"fmt"
	"io"
	"net/http"
	"os"
)

func main() {
	body := []byte(`{
	  "model": "claude-sonnet-5",
	  "messages": [
	    {"role": "system", "content": "You are a friendly, concise assistant."},
	    {"role": "user", "content": "Write a haiku about the sea."}
	  ],
	  "stream": true
	}`)
	req, _ := http.NewRequest("POST", "https://gateway-production-c837.up.railway.app/v1/chat/completions", bytes.NewReader(body))
	req.Header.Set("Authorization", "Bearer "+os.Getenv("SOMNUS_API_KEY"))
	req.Header.Set("Content-Type", "application/json")
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer res.Body.Close()
	out, _ := io.ReadAll(res.Body)
	fmt.Println(string(out))
}

JSON output

go
package main

import (
	"bytes"
	"fmt"
	"io"
	"net/http"
	"os"
)

func main() {
	body := []byte(`{
	  "model": "claude-sonnet-5",
	  "messages": [
	    {"role": "system", "content": "Reply only with JSON."},
	    {"role": "user", "content": "Return {\"city\": ..., \"country\": ...} for the Eiffel Tower."}
	  ],
	  "response_format": {"type": "json_object"}
	}`)
	req, _ := http.NewRequest("POST", "https://gateway-production-c837.up.railway.app/v1/chat/completions", bytes.NewReader(body))
	req.Header.Set("Authorization", "Bearer "+os.Getenv("SOMNUS_API_KEY"))
	req.Header.Set("Content-Type", "application/json")
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer res.Body.Close()
	out, _ := io.ReadAll(res.Body)
	fmt.Println(string(out))
}

Go notes

  • Set a request timeout on your http.Client.
  • Decode only the fields you need from the response.
  • Reuse one client for connection pooling.

Questions

Which models can I use?

Any of the 50+ models on Somnus — change the model string. The examples use claude-sonnet-5.

Where do I get a key?

Create an account, and your key appears in the dashboard with $5 of free credit.

Run this code with free credit

Create an account and get $5 of free API credit — enough to try every example on this page.

Get $5 free credit

Related

Browse more: Alternatives · Compare · Use cases · Developers · Solutions · Tools

WindowsStart with $5 free