Knock API access for applications using .NET. Supports .NET Standard 2.0+ and .NET Framework 4.6.1+.
See the API documentation for usage examples.
There are several options to install the Knock .NET SDK.
nuget install Knock.net
dotnet add package Knock.net
Install-Package Knock.net
To use the Knock client, you must provide an API key from the Knock dashboard.
using Knock;
var knockClient = new KnockClient(
new KnockOptions { ApiKey = "sk_12345" });
var knockClient = new KnockClient(
new KnockOptions { ApiKey = "sk_12345" });
var identifyUserParams = new Dictionary<string, object>{
{ "name", "John Hammond" },
{ "email", "[email protected]" }
};
var user = await knockClient.Users.Identify("jhammond", identifyUserParams)
var knockClient = new KnockClient(
new KnockOptions { ApiKey = "sk_12345" });
var user = await knockClient.Users.Get("jhammond")
var knockClient = new KnockClient(
new KnockOptions { ApiKey = "sk_12345" });
var workflowTrigger = new TriggerWorkflow {
// list of user ids for who should receive the notif
Recipients = ["jhammond", "agrant", "imalcolm", "esattler"],
// user id of who performed the action
Actor: "dnedry",
// an optional cancellation key
CancellationKey: alert.Id,
// an optional tenant
Tenant: "jurassic-park",
// data payload to send through
Data: new Dictionary<string, object>{
{"type", "trex"},
{"priority", "1"}
},
};
var result = await knockClient.Workflows.Trigger("dinosaurs-loose", workflowTrigger)
var knockClient = new KnockClient(
new KnockOptions { ApiKey = "sk_12345" });
// Set preference set for user
var preferenceSetUpdate = new SetPreferenceSet {
ChannelTypes = new Dictionary<string, boolean> {
{"email", false}
}
};
var result = await knockClient.Users.SetPreferences("jhammond", preferenceSetUpdate);
// Set granular channel type preferences
var result = await knockClient.Users.SetChannelTypePreferences("jhammond", "email", true);
// Set granular workflow preferences
var result = await knockClient.Users.SetWorkflowPreferences("jhammond", "dinosaurs-loose", false);
// Retrieve preferences
var preferenceSet = await knockClient.Users.GetPreferences("jhammond");
var knockClient = new KnockClient(
new KnockOptions { ApiKey = "sk_12345" });
// Set channel data for an APNS channel
var channelData = new Dictionary<string, List<string>>{
{"tokens", [apnsToken]},
};
var result = await knockClient.Users.SetChannelData("jhammond", knockApnsChannelId, channelData);
// Get channel data for the APNS channel
var result = await knockClient.Users.GetChannelData("jhammond", knockApnsChannelId);
var knockClient = new KnockClient(
new KnockOptions { ApiKey = "sk_12345" });
var cancelParams = new CancelWorkflow {
// Optional list of recipients to cancel the workflow run for
Recipients = ["dnedry"],
// The cancellation key that corresponds with the original workflow run
CancellationKey = alert.id,
};
var result = await knockClient.Workflows.cancel("dinosaurs-loose", cancelParams);