GoLang API Client for Twitch API, EventSub, and WebSockets
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
maxpeterkaya ee99c8722b
All checks were successful
CI / build (push) Successful in 4s
docs: add README
2026-08-19 18:23:10 -04:00
.forgejo/workflows ci: add release changelog workflow 2026-08-19 18:22:56 -04:00
.gitignore chore: initialize Go module and gitignore 2026-08-19 18:22:54 -04:00
events.go feat: add Helix and EventSub types 2026-08-19 18:22:58 -04:00
go.mod chore: initialize Go module and gitignore 2026-08-19 18:22:54 -04:00
go.sum chore: initialize Go module and gitignore 2026-08-19 18:22:54 -04:00
helix.go feat: add session core with websocket and OAuth 2026-08-19 18:23:00 -04:00
helix_chat.go feat: add Helix user, stream, video, and chat endpoints 2026-08-19 18:23:02 -04:00
helix_eventsub.go feat: add EventSub subscription management 2026-08-19 18:23:03 -04:00
helix_stream.go feat: add Helix user, stream, video, and chat endpoints 2026-08-19 18:23:02 -04:00
helix_types.go feat: add Helix and EventSub types 2026-08-19 18:22:58 -04:00
helix_user.go feat: add Helix user, stream, video, and chat endpoints 2026-08-19 18:23:02 -04:00
helix_video.go feat: add Helix user, stream, video, and chat endpoints 2026-08-19 18:23:02 -04:00
http.go feat: add session core with websocket and OAuth 2026-08-19 18:23:00 -04:00
LICENSE init license 2026-08-19 18:21:09 -04:00
oauth.go feat: add session core with websocket and OAuth 2026-08-19 18:23:00 -04:00
README.md docs: add README 2026-08-19 18:23:10 -04:00
subscription.go feat: add EventSub subscription management 2026-08-19 18:23:03 -04:00
subscription_types.go feat: add Helix and EventSub types 2026-08-19 18:22:58 -04:00
twitch.go feat: add session core with websocket and OAuth 2026-08-19 18:23:00 -04:00
twitch_test.go test: add session integration test 2026-08-19 18:23:08 -04:00
webhook.go feat: add webhook delivery handler 2026-08-19 18:23:05 -04:00
webhook_test.go feat: add webhook delivery handler 2026-08-19 18:23:05 -04:00
websocket_api.go feat: add session core with websocket and OAuth 2026-08-19 18:23:00 -04:00
websocket_event.go feat: add session core with websocket and OAuth 2026-08-19 18:23:00 -04:00
websocket_types.go feat: add Helix and EventSub types 2026-08-19 18:22:58 -04:00

gotwitch

gotwitch is a Go client library for the Twitch API. It covers the Helix REST API and the EventSub websocket for building chat bots and stream tools.

Installation

go get vc.maxkaya.com/sdk/go-twitch

Quick Start

client := gotwitch.TwitchClient{
    ClientID:       os.Getenv("TWITCH_CLIENT_ID"), // Specific app Client ID; for app token
    ClientSecret:   os.Getenv("TWITCH_CLIENT_SECRET"), // Same as above
    BotUsername:    os.Getenv("TWITCH_BOT_USERNAME"),
    BotAccessToken: os.Getenv("TWITCH_BOT_ACCESS_TOKEN"),
    Channels:       []string{"channelname"},
}

session, err := gotwitch.New(client)
if err != nil {
    log.Fatal(err)
}
session.RegisterEventListener("channel.chat.message", func(e gotwitch.WebsocketMessage) {
    fmt.Println(e.Payload.Event.Message.Text)
})
if err := session.Connect(); err != nil {
    log.Fatal(err)
}

Websocket Subscriptions

Events arrive over the EventSub websocket after Connect establishes a session. Create a subscription for every channel in the TwitchClient Channels field.

session.CreateSubscription(gotwitch.SubscriptionTypeChannelChatMessage, gotwitch.Transport{Method: "websocket"})

Webhook Subscriptions

Events arrive as HTTP requests to a public callback URL. Webhook subscriptions need a callback URL reachable by Twitch over HTTPS and a shared secret.

session.CreateSubscription(gotwitch.SubscriptionTypeChannelFollow, gotwitch.Transport{Method: "webhook", Callback: "https://example.com/twitch", Secret: "s3cret"})

Serve the callback from your own HTTP server. NewWebhookHandler answers Twitch verification requests and only forwards signed notifications to your handler.

mux := http.NewServeMux()
mux.Handle("/twitch/events", gotwitch.NewWebhookHandler("s3cret", func(rw http.ResponseWriter, req *http.Request, message gotwitch.WebsocketMessage) {
    fmt.Println(message.Metadata.SubscriptionType)
}))
http.ListenAndServe(":8080", mux)

Documentation

Full API reference with examples lives in the godoc comments. Browse with go doc gotwitch or pkg.go.dev.

License

MIT