Setting preferences

You can read more about modeling preferences and preferences in general in the Preferences guide. In the guide below, we walk through the technical details of integrating the Knock preferences model into your product.

Preferences live under a user or an object and are used to structure a set of rules about what notifications a recipient should receive (called a PreferenceSet). They are designed to be flexible and expressive to accommodate complex scenarios like per-resource, channel preferences (like opting out of an individual Slack channel).

Backend integration

In the following examples you'll see how to integrate preferences into your backend codebase.

Getting preferences

You can retrieve a given set of preferences using the getPreferences method. Please note that even if the preferences are not set, you will receive an empty PreferenceSet in response.

Setting preferences

You can update an entire set of preferences using the setPreferences method. This will overwrite any existing preferences set:

Setting preferences with conditions

You can use the setPreferences method to also set preferences with conditions that are evaluated. Please refer to the documentation on preference conditions to learn more.

Example an example. Follow this link to see an example of how to set preferences with conditions.

Bulk setting user preferences

You can update the preferences of up to 100 users in a single batch by using the users.bulkSetPreferences method. This executes an asynchronous job which will overwrite any existing preferences for the users provided. You can track the progress of the BulkOperation returned via the bulk operation API.

See an example. Check out our example app to see the bulk preferences API being used within the context of a collaboration app.

Client integration

The preferences API can also be invoked from the client, providing you have authenticated a user first and have a valid API token.

Note: at this time only the current user's preferences can be retrieved or updated.

Retrieve the preferences for the current user

Set the preferences for the current user

See an example. Check out our example app for real examples of getting and setting preferences using our client library.