Knock API access for applications written in Python.
See the documentation for Python usage examples.
To install from PyPi, run the following:
pip install knockapi
To install from source, clone the repo and run the following:
python setup.py install
To use the library you must provide a secret API key, provided in the Knock dashboard.
from knockapi import Knock
client = Knock(api_key="sk_12345")
from knockapi import Knock
client = Knock(api_key="sk_12345")
client.users.identify(id="jhammond", data={"name": "John Hammond", "email": "[email protected]"})
from knockapi import Knock
client = Knock(api_key="sk_12345")
client.users.get_user(id="jhammond")
from knockapi import Knock
client = Knock(api_key="sk_12345")
client.notify(
key="dinosaurs-loose",
actor="dnedry",
recipients=["jhammond", "agrant", "imalcolm", "esattler"],
cancellation_key=alert.id,
tenant="jurassic-park",
data={
"type": "trex",
"priority": 1
}
)
from knockapi import Knock
client = Knock(api_key="sk_12345")
client.workflows.cancel(
key="dinosaurs-loose",
cancellation_key=alert.id,
recipients=["jhammond", "agrant", "imalcolm", "esattler"],
)
from knockapi import Knock
client = Knock(api_key="sk_12345")
# Replaces the preferences for the user
client.preferences.update(
user_id="jhammond",
channel_types={'email': True},
workflows={'dinosaurs-loose': False}
)
# Retrieve the current preferences
client.preferences.get(user_id="jhammond")
You can use the pyjwt
package to sign JWTs easily.
You will need to generate an environment specific signing key, which you can find in the Knock dashboard.
If you're using a signing token you will need to pass this to your client to perform authentication. You can read more about client-side authentication here.
import jwt
import os
private_key = os.getenv("KNOCK_SIGNING_KEY")
encoded = jwt.encode({"sub": "jhammond"}, private_key, algorithm="RS256")