Skip to content

apinator-io/sdk-go

Repository files navigation

Apinator Go SDK

Go Reference CI License: MIT

Go server SDK for Apinator — trigger real-time events, authenticate channels.

Features

  • Trigger events to one or more channels
  • Authenticate private and presence channel subscriptions
  • Verify incoming webhook signatures
  • Query channel state and subscription counts
  • HMAC-SHA256 request signing
  • Zero external dependencies (stdlib only)
  • Go 1.21+

Installation

go get github.com/apinator-io/sdk-go

Quick Start

package main

import (
	"context"
	"log"

	apinator "github.com/apinator-io/sdk-go"
)

func main() {
	client := apinator.NewClient(
		"your-app-id",
		"your-key",
		"your-secret",
		"eu", // cluster
	)

	err := client.Trigger(context.Background(), apinator.TriggerParams{
		Name:    "my-event",
		Channel: "my-channel",
		Data:    `{"message": "hello world"}`,
	})
	if err != nil {
		log.Fatal(err)
	}
}

Channel Authentication

Authenticate private and presence channel subscriptions from your server:

package main

import (
	"encoding/json"
	"net/http"

	apinator "github.com/apinator-io/sdk-go"
)

func main() {
	client := apinator.NewClient("app-id", "key", "secret", "eu")

	http.HandleFunc("/realtime/auth", func(w http.ResponseWriter, r *http.Request) {
		if err := r.ParseForm(); err != nil {
			http.Error(w, "bad request", http.StatusBadRequest)
			return
		}

		socketID := r.FormValue("socket_id")
		channelName := r.FormValue("channel_name")

		auth := client.AuthenticateChannel(socketID, channelName, nil)

		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(auth)
	})

	http.ListenAndServe(":8080", nil)
}

For presence channels, include channel_data:

channelData := `{"user_id": "123", "user_info": {"name": "Alice"}}`
auth := client.AuthenticateChannel(socketID, channelName, &channelData)

Webhook Verification

Verify that incoming webhooks are authentic:

package main

import (
	"io"
	"net/http"
	"time"

	apinator "github.com/apinator-io/sdk-go"
)

func main() {
	client := apinator.NewClient("app-id", "key", "secret", "eu")

	http.HandleFunc("/webhooks", func(w http.ResponseWriter, r *http.Request) {
		body, err := io.ReadAll(r.Body)
		if err != nil {
			http.Error(w, "bad request", http.StatusBadRequest)
			return
		}

		if !client.VerifyWebhook(r.Header, body, 5*time.Minute) {
			http.Error(w, "invalid signature", http.StatusUnauthorized)
			return
		}

		// Process the webhook payload...
		w.WriteHeader(http.StatusOK)
	})

	http.ListenAndServe(":8080", nil)
}

Channel Introspection

Query active channels and their subscription counts:

// List all channels
channels, err := client.GetChannels(ctx, "")

// Filter by prefix
channels, err := client.GetChannels(ctx, "presence-")

// Get a specific channel
channel, err := client.GetChannel(ctx, "my-channel")
if channel != nil {
	fmt.Printf("Channel: %s, Subscribers: %d\n", channel.Name, channel.SubscriptionCount)
}

Options

// Use a custom HTTP client
client := apinator.NewClient("app-id", "key", "secret", "eu",
	apinator.WithHTTPClient(&http.Client{
		Timeout: 10 * time.Second,
	}),
)

Links

About

No description, website, or topics provided.

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages