Skip to content

Getting Started: Go

Use Go (1.20+) to call the BetterPrompt API.

go
package main

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

func main() {
	apiKey := os.Getenv("BETTERPROMPT_API_KEY")
	if apiKey == "" {
		apiKey = "YOUR_API_KEY"
	}
	promptVersionId := os.Getenv("BP_PROMPT_VERSION_ID")
	if promptVersionId == "" {
		promptVersionId = "YOUR_PROMPT_VERSION_ID"
	}

	body, _ := json.Marshal(map[string]interface{}{
		"promptVersionId": promptVersionId,
		"inputs": map[string]interface{}{
			"textInputs": map[string]string{"yourField": "value"},
		},
	})

	req, _ := http.NewRequest("POST", "https://api.betterprompt.me/v1/runs", bytes.NewBuffer(body))
	req.Header.Set("Authorization", "Bearer "+apiKey)
	req.Header.Set("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	var out map[string]interface{}
	json.NewDecoder(resp.Body).Decode(&out)
	fmt.Println(out)
}