Firmreader Docs Online logo

Go

Impara a installare e usare l'SDK Go di Firmreader (Go 1.21 o superiore) per creare post, elencarli e gestire gli errori dell'API.

L'SDK Go richiede Go 1.21 o superiore. Ogni metodo accetta un context.Context, così puoi applicare timeout e annullamenti.

Installazione

go get github.com/firmreader/firmreader-go

Inizializzare il client

package main

import (
	"os"

	"github.com/firmreader/firmreader-go"
)

func main() {
	client := firmreader.NewClient(os.Getenv("FIRMREADER_API_KEY")) // fr_live_...
	_ = client
}

Creare un post

post, err := client.Posts.Create(ctx, &firmreader.PostParams{
	ChannelID: "ch_company_news",
	Title:     "Welcome to Firmreader",
	Body:      "We're excited to announce our new internal communications platform.",
	Priority:  "normal",
})
if err != nil {
	log.Fatal(err)
}

fmt.Printf("Published post %s\n", post.ID)

Elencare i post

posts, err := client.Posts.List(ctx, &firmreader.ListParams{
	ChannelID: "ch_company_news",
	Limit:     20,
})
if err != nil {
	log.Fatal(err)
}

for _, post := range posts.Data {
	fmt.Println(post.Title)
}

Gestire gli errori

Il client restituisce un *firmreader.Error per le risposte non 2xx. Usa errors.As per leggere lo stato e il codice.

var apiErr *firmreader.Error
if errors.As(err, &apiErr) {
	fmt.Printf("%d: %s\n", apiErr.Status, apiErr.Code)
}

Imposta firmreader.WithIdempotencyKey("...") su qualsiasi chiamata di scrittura, in modo che i nuovi tentativi non creino post duplicati.

Last updated on