go get github.com/GetStream/getstream-go/v4Getting Started
Setup
The official Go SDK for Stream covers Chat, Video, Moderation, and Feeds.
Install the SDK:
Initialize the client with your API key and secret (available on the Dashboard):
import "github.com/GetStream/getstream-go/v4"
client, err := getstream.NewClient("your-api-key", "your-api-secret")
if err != nil {
panic(err)
}You can also load credentials from environment variables (STREAM_API_KEY, STREAM_API_SECRET):
client, err := getstream.NewClientFromEnvVars()Server-side Token
Your backend generates a user token that the client SDKs use to authenticate. A typical place to issue this token is during login or registration.
token, err := client.CreateToken("user-id")
if err != nil {
panic(err)
}
// return the token to the client appTokens with an expiry:
token, err := client.CreateToken("user-id",
getstream.WithExpiration(24 * time.Hour))Making Your First API Call
Create a user, open a channel, and send a message:
// Upsert a user
client.UpdateUsers(ctx, &getstream.UpdateUsersRequest{
Users: map[string]getstream.UserRequest{
"john": {ID: "john", Name: getstream.PtrTo("John")},
},
})
// Create or join a channel
channel := client.Chat().Channel("messaging", "hello-world")
channel.GetOrCreate(ctx, &getstream.GetOrCreateChannelRequest{
Data: &getstream.ChannelInput{
CreatedByID: getstream.PtrTo("john"),
},
})
// Send a message
channel.SendMessage(ctx, &getstream.SendMessageRequest{
Message: getstream.MessageRequest{
Text: getstream.PtrTo("Hello, Stream!"),
UserID: getstream.PtrTo("john"),
},
})