Skip to main content
Integrate px0 into your Go application. This guide shows how to fetch and render a prompt template programmatically.

Prerequisites

Before running the SDK, ensure you have completed the following steps:

Configuration

The Go SDK requires connection details and authentication to communicate with your px0 instance. Configure these details using environment variables.
  • PX0_HOST: The URL of your px0 API instance.
  • PX0_ACCESS_TOKEN: Your API access key.
Set these variables in your shell before running the applications:
export PX0_HOST="http://localhost:8000"
export PX0_ACCESS_TOKEN="your_access_key_here"

Installation

Install the required client library for Go.
go get github.com/px0-ai/px0-go

Hello World Example

The following example demonstrates how to load the client, authenticate, and render a live prompt template named hello_world with a dynamic name variable.
package main

import (
	"context"
	"fmt"
	"os"

	"github.com/px0-ai/px0-go"
)

func main() {
	host := os.Getenv("PX0_HOST")
	accessToken := os.Getenv("PX0_ACCESS_TOKEN")

	config := px0.NewConfiguration()
	if host != "" {
		config.Servers = px0.ServerConfigurations{
			{
				URL: host,
			},
		}
	}

	apiClient := px0.NewAPIClient(config)

	ctx := context.Background()
	if accessToken != "" {
		ctx = context.WithValue(ctx, px0.ContextAccessToken, accessToken)
	}

	renderRequest := px0.NewRenderRequest()
	renderRequest.SetVariables(map[string]interface{}{
		"name": "World",
	})

	response, _, err := apiClient.PromptRendersAPI.RenderLive(ctx, "hello_world").
		RenderRequest(*renderRequest).
		Execute()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error rendering live: %v\n", err)
		os.Exit(1)
	}

	fmt.Println(response.GetRendered())
}

Execution

Run the Go application:
go run main.go