https://github.com/knocklabs/knock-dotnet
Official .NET SDK for interacting with the Knock API
https://github.com/knocklabs/knock-dotnet
csharp dotnet knock notifications
Last synced: about 2 months ago
JSON representation
Official .NET SDK for interacting with the Knock API
- Host: GitHub
- URL: https://github.com/knocklabs/knock-dotnet
- Owner: knocklabs
- License: mit
- Created: 2021-11-23T15:00:12.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2025-06-11T16:47:23.000Z (about 1 year ago)
- Last Synced: 2025-09-20T22:44:35.040Z (9 months ago)
- Topics: csharp, dotnet, knock, notifications
- Language: C#
- Homepage: https://github.com/knocklabs/knock-dotnet/
- Size: 96.7 KB
- Stars: 5
- Watchers: 7
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# Knock .NET Library
Knock API access for applications using .NET. Supports .NET Standard 2.0+ and .NET Framework 4.6.1+.
## Documentation
See the [API documentation](https://docs.knock.app) for usage examples.
## Installation
There are several options to install the Knock .NET SDK.
### Via the NuGet Package Manager
```
nuget install Knock.net
```
### Via the .NET Core Command Line Tools
```
dotnet add package Knock.net
```
### Via Visual Studio IDE
```
Install-Package Knock.net
```
## Configuration
To use the Knock client, you must provide an API key from the Knock dashboard.
```c#
using Knock;
var knockClient = new KnockClient(
new KnockOptions { ApiKey = "sk_12345" });
```
## Usage
### Identifying users
```c#
var knockClient = new KnockClient(
new KnockOptions { ApiKey = "sk_12345" });
var identifyUserParams = new Dictionary{
{ "name", "John Hammond" },
{ "email", "jhammond@ingen.net" }
};
var user = await knockClient.Users.Identify("jhammond", identifyUserParams)
```
### Retrieving users
```c#
var knockClient = new KnockClient(
new KnockOptions { ApiKey = "sk_12345" });
var user = await knockClient.Users.Get("jhammond")
```
### Triggering workflows
```c#
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{
{"type", "trex"},
{"priority", "1"}
},
};
var result = await knockClient.Workflows.Trigger("dinosaurs-loose", workflowTrigger)
```
### Preferences
```c#
var knockClient = new KnockClient(
new KnockOptions { ApiKey = "sk_12345" });
// Set preference set for user
var preferenceSetUpdate = new SetPreferenceSet {
ChannelTypes = new Dictionary {
{"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");
```
### Getting and setting channel data
```c#
var knockClient = new KnockClient(
new KnockOptions { ApiKey = "sk_12345" });
// Set channel data for an APNS channel
var channelData = new Dictionary>{
{"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);
```
### Canceling notifies
```c#
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);
```