Skip to main content

Client library for the Knock API

Project description

Knock Python library

Knock API access for applications written in Python.

Documentation

See the documentation for Python usage examples.

Installation

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

Configuration

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")

Usage

Identifying users

from knockapi import Knock
client = Knock(api_key="sk_12345")

client.users.identify(id="jhammond", data={"name": "John Hammond", "email": "jhammond@ingen.net"})

Retrieving users

from knockapi import Knock
client = Knock(api_key="sk_12345")

client.users.get_user(id="jhammond")

Sending notifies

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
  }
)

Getting and setting channel data

from knockapi import Knock
client = Knock(api_key="sk_12345")

# Set channel data for an APNS
client.users.set_channel_data(
  id="jhammond",
  channel_id=KNOCK_APNS_CHANNEL_ID,
  channel_data={
    "tokens": [apns_token]
  }
)

# Get channel data for the APNS channel
client.users.get_channel_data(id="jhammond", channel_id=KNOCK_APNS_CHANNEL_ID)

Canceling notifies

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"],
)

User preferences

from knockapi import Knock
client = Knock(api_key="sk_12345")

# Replaces the preferences for the user
client.users.set_preferences(
  user_id="jhammond",
  channel_types={'email': True},
  workflows={'dinosaurs-loose': False}
)

# Retrieve the current preferences
client.users.get_preferences(user_id="jhammond")

Signing JWTs

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")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

knockapi-0.4.1.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

knockapi-0.4.1-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file knockapi-0.4.1.tar.gz.

File metadata

  • Download URL: knockapi-0.4.1.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10

File hashes

Hashes for knockapi-0.4.1.tar.gz
Algorithm Hash digest
SHA256 40542d07da34f014501b18e4f05f65f50b00831b56c0a722356ca120648efefc
MD5 4838b282ae52e446050588f680be494d
BLAKE2b-256 06387009a32ea58dacf9197d06b927e0bc17b6524e2c3ab856ec4eee17a21b95

See more details on using hashes here.

File details

Details for the file knockapi-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: knockapi-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10

File hashes

Hashes for knockapi-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 caf4bf1256320744927947e1a77279133ec59cb7a313d140a899215fd0291c4b
MD5 78ee1bd6e94680b4a313d763b1430c40
BLAKE2b-256 f7e5ee13922d407897b7d2a81d091ef59d60a8c8ccf214953aefc6f1d03ca67b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page