Skip to main content

Basic usage

package main

import (
  "bytes"
  "encoding/json"
  "fmt"
  "io"
  "net/http"
)

func main() {
  url := "https://api.zerogpu.ai/v1/responses"

  payload := map[string]any{
    "model": "YOUR_MODEL",
    "input": []map[string]string{
      {
        "role":    "user",
        "content": "Your input text here...",
      },
    },
    "text": map[string]any{
      "format": map[string]string{
        "type": "text",
      },
    },
  }

  payloadBytes, err := json.Marshal(payload)
  if err != nil {
    panic(err)
  }

  req, err := http.NewRequest("POST", url, bytes.NewBuffer(payloadBytes))
  if err != nil {
    panic(err)
  }

  req.Header.Set("content-type", "application/json")
  req.Header.Set("x-api-key", "YOUR_API_KEY")
  req.Header.Set("x-project-id", "YOUR_PROJECT_ID")

  client := &http.Client{}
  res, err := client.Do(req)
  if err != nil {
    panic(err)
  }
  defer res.Body.Close()

  body, _ := io.ReadAll(res.Body)
  fmt.Println(string(body))
}

Using environment variables

import "os"

apiKey := os.Getenv("ZEROGPU_API_KEY")
projectID := os.Getenv("ZEROGPU_PROJECT_ID")

req.Header.Set("x-api-key", apiKey)
req.Header.Set("x-project-id", projectID)

Error handling

res, err := client.Do(req)
if err != nil {
  log.Fatalf("Request failed: %v", err)
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
  body, _ := io.ReadAll(res.Body)
  log.Fatalf("API error %d: %s", res.StatusCode, string(body))
}