Skip to content

Official Go SDK for interacting with the Knock API.

License

Notifications You must be signed in to change notification settings

knocklabs/knock-go

Folders and files

NameName
Last commit message
Last commit date
Jun 16, 2022
Feb 26, 2025
May 31, 2022
Apr 7, 2023
Apr 6, 2023
Apr 7, 2023
Jun 8, 2022
May 16, 2024
Jun 3, 2022
May 18, 2022

Repository files navigation

Knock Go Library

Knock API access for applications written in Go.

Documentation

See the documentation for Go usage examples.

Installation

go get github.com/knocklabs/knock-go/knock

Configuration

To use the library you must provide a secret API key, provided in the Knock dashboard. You must pass it into the client:

package main

import (
    "context"
    "fmt"
    "os"

    "github.com/knocklabs/knock-go/knock"
)

func main() {
    token := os.Getenv("KNOCK_TOKEN")

    ctx := context.Background()

    // create a new Knock API client with the given access token
    client, _ := knock.NewClient(
        knock.WithAccessToken(token),
    )

    // any example code from the rest of the readme can be run here

Identifying Users

user, _ := client.Users.Identify(ctx, &knock.IdentifyUserRequest{
    ID:   "fun-user2",
    Name: "John Hammond",
    CustomProperties: map[string]interface{}{
        "welcome":     "to jurassic park",
        "middle-name": "alfred",
    },
})
fmt.Printf("user-name: %+v\n", user)

Sending notifies (triggering workflows)

Simple trigger for one recipient by id:

req := &knock.TriggerWorkflowRequest{
    Workflow:   "test",
    Data: map[string]interface{}{
        "life":      "found a way",
        "dinosaurs": "loose",
    },
}
req.AddRecipientByID("tim")
workflow, _ := client.Workflows.Trigger(ctx, req, nil)
fmt.Printf("workflow: %+v\n", workflow)

Trigger with inline-identified recipient:

req := &knock.TriggerWorkflowRequest{
    Workflow:   "test",
    Data: map[string]interface{}{
        "life":      "found a way",
        "dinosaurs": "loose",
    },
}
req.AddRecipientByEntity(map[string]interface{}{
    "id":    "dnedry",
    "name":  "Dennis",
    "email": "[email protected]",
})
workflow, _ := client.Workflows.Trigger(ctx, req, nil)
fmt.Printf("workflow: %+v\n", workflow)

Trigger for multiple recipients and an object

req := &knock.TriggerWorkflowRequest{
    Workflow:   "test",
    Data: map[string]interface{}{
        "life":      "found a way",
        "dinosaurs": "loose",
    },
}

for _, r := range []string{"tim", "hammond"} {
    req.AddRecipientByID(r)
}

req.AddRecipientByEntity(map[string]interface{}{
    "id": "group-a",
    "collection": "groups",
})
workflow, _ := client.Workflows.Trigger(ctx, req, nil)
fmt.Printf("workflow: %+v\n", workflow)

Trigger with idempotency key

req := &knock.TriggerWorkflowRequest{
    Workflow:   "test",
    Data: map[string]interface{}{
        "life":      "found a way",
        "dinosaurs": "loose",
    },
}
req.AddRecipientByID("tim")
workflow, _ := client.Workflows.Trigger(ctx, req, &knock.MethodOptions{
    IdempotencyKey: "an-idempotency-key",
})
fmt.Printf("workflow: %+v\n", workflow)

Retrieving users

user, _ = client.Users.Get(ctx, &knock.GetUserRequest{
    ID: "fun-user",
})

fmt.Printf("retrieved-user: %+v\n", user)

Deleting users

client.Users.Delete(ctx, &knock.DeleteUserRequest{
    ID: "fun-user",
})

Getting and setting channel data

setUserChannelDataResponse, _ := client.Users.SetChannelData(ctx, &knock.SetUserChannelDataRequest{
    UserID:    "test-123",
    ChannelID: "5d2377a0-92fb-4616-8315-eee843556566",
    Data: map[string]interface{}{
        "tokens": []string{"a", "b"},
    },
})
fmt.Printf("channel-data-set: %+v\n", setUserChannelDataResponse)

getUserChannelDataResponse, _ := client.Users.GetChannelData(ctx, &knock.GetUserChannelDataRequest{
    UserID:    "test-123",
    ChannelID: "5d2377a0-92fb-4616-8315-eee843556566",
})
fmt.Printf("channel-data-found: %+v\n", getUserChannelDataResponse)

Canceling workflows

client.Workflows.Cancel(ctx, &knock.CancelWorkflowRequest{
    Workflow:        "test",
    CancellationKey: "user-123",
})
}