Install
openclaw skills install @psyb0t/go-perplexityUnofficial Go client for the Perplexity REST API at https://api.perplexity.ai. perplexity.NewClientWithResponses from github.com/psyb0t/go-perplexity gives one typed method per endpoint, generated with oapi-codegen from Perplexity's OpenAPI document - Sonar chat completions (sync and async), web search, embeddings and contextualized embeddings, agents and their files, skills, decisions, models and usage analytics - with JSON bodies decoded into Go structs. Auth is a bearer API key set through a request editor. Use when Go code needs to call the Perplexity API or already imports go-perplexity.
openclaw skills install @psyb0t/go-perplexityTyped Go client for the Perplexity API. Every endpoint in Perplexity's OpenAPI document is a method on ClientWithResponses.
For install, the method list, union types and error handling, see references/setup.md.
PERPLEXITY_API_KEY. Never hardcode it, commit it or log it, and never print request headers.NewClientWithResponses. Use https://api.perplexity.ai.github.com/psyb0t/go-perplexity.Client method, which returns the *http.Response with the body unread, and parse the stream yourself.import (
"context"
"fmt"
"net/http"
"os"
"github.com/psyb0t/go-perplexity"
)
auth := perplexity.WithRequestEditorFn(func(_ context.Context, req *http.Request) error {
req.Header.Set("Authorization", "Bearer "+os.Getenv("PERPLEXITY_API_KEY"))
return nil
})
client, err := perplexity.NewClientWithResponses("https://api.perplexity.ai", auth)
if err != nil {
return err
}
A Sonar chat completion. Message content is a union, built and read with From... and As... helpers (Content0 is plain text):
var content perplexity.ChatMessageInput_Content
if err := content.FromChatMessageInputContent0("What changed in Go 1.26?"); err != nil {
return err
}
res, err := client.ChatCompletionsChatCompletionsPostWithResponse(ctx, perplexity.ApiChatCompletionsRequest{
Model: perplexity.ApiChatCompletionsRequestModelSonar,
Messages: []perplexity.ChatMessageInput{{
Content: &content,
Role: perplexity.ChatMessageRoleUser,
}},
})
if err != nil {
return err // transport error
}
if res.JSON200 == nil {
return fmt.Errorf("sonar: status %d: %s", res.StatusCode(), res.Body)
}
for _, choice := range res.JSON200.Choices {
if choice.Message.Content == nil {
continue
}
text, err := choice.Message.Content.AsChatMessageOutputContent0()
if err == nil {
fmt.Println(text)
}
}
A web search:
var query perplexity.ApiSearchRequest_Query
if err := query.FromApiSearchRequestQuery0("go generics tutorial"); err != nil {
return err
}
maxResults := 5
found, err := client.SearchSearchPostWithResponse(ctx, perplexity.ApiSearchRequest{
Query: query,
MaxResults: &maxResults,
})
if err != nil {
return err
}
if found.JSON200 == nil {
return fmt.Errorf("search: status %d: %s", found.StatusCode(), found.Body)
}
for _, page := range found.JSON200.Results {
fmt.Println(page.Title, page.Url)
}