Python Client SDK Generated by Speakeasy.
Project description
novu-py
Developer-friendly & type-safe Python SDK specifically catered to leverage novu-py API.
Summary
Novu API: Novu REST API. Please see https://docs.novu.co/api-reference for more details.
For more information about the API: Novu Documentation
Table of Contents
SDK Installation
[!NOTE] Python version upgrade policy
Once a Python version reaches its official end of life date, a 3-month grace period is provided for users to upgrade. Following this grace period, the minimum python version supported in the SDK will be updated.
The SDK can be installed with either pip or poetry package managers.
PIP
PIP is the default package installer for Python, enabling easy installation and management of packages from PyPI via the command line.
pip install novu-py
Poetry
Poetry is a modern tool that simplifies dependency management and package publishing by using a single pyproject.toml file to handle project metadata and dependencies.
poetry add novu-py
Shell and script usage with uv
You can use this SDK in a Python shell with uv and the uvx command that comes with it like so:
uvx --from novu-py python
It's also possible to write a standalone Python script without needing to set up a whole project like so:
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.9"
# dependencies = [
# "novu-py",
# ]
# ///
from novu_py import Novu
sdk = Novu(
# SDK arguments
)
# Rest of script here...
Once that is saved to a file, you can run it with uv run script.py where
script.py can be replaced with the actual file name.
IDE Support
PyCharm
Generally, the SDK will work well with most IDEs out of the box. However, when using PyCharm, you can enjoy much better integration with Pydantic by installing an additional plugin.
SDK Example Usage
Trigger Notification Event
# Synchronous Example
import novu_py
from novu_py import Novu
import os
with Novu(
secret_key=os.getenv("NOVU_SECRET_KEY", ""),
) as novu:
res = novu.trigger(trigger_event_request_dto=novu_py.TriggerEventRequestDto(
workflow_id="workflow_identifier",
to={
"subscriber_id": "<id>",
},
payload={
"comment_id": "string",
"post": {
"text": "string",
},
},
overrides={
"fcm": {
"data": {
"key": "value",
},
},
},
))
# Handle response
print(res)
The same SDK client can also be used to make asychronous requests by importing asyncio.
# Asynchronous Example
import asyncio
import novu_py
from novu_py import Novu
import os
async def main():
async with Novu(
secret_key=os.getenv("NOVU_SECRET_KEY", ""),
) as novu:
res = await novu.trigger_async(trigger_event_request_dto=novu_py.TriggerEventRequestDto(
workflow_id="workflow_identifier",
to={
"subscriber_id": "<id>",
},
payload={
"comment_id": "string",
"post": {
"text": "string",
},
},
overrides={
"fcm": {
"data": {
"key": "value",
},
},
},
))
# Handle response
print(res)
asyncio.run(main())
Trigger Notification Events in Bulk
# Synchronous Example
import novu_py
from novu_py import Novu
import os
with Novu(
secret_key=os.getenv("NOVU_SECRET_KEY", ""),
) as novu:
res = novu.trigger_bulk(bulk_trigger_event_dto={
"events": [
novu_py.TriggerEventRequestDto(
workflow_id="workflow_identifier",
to={
"subscriber_id": "<id>",
},
payload={
"comment_id": "string",
"post": {
"text": "string",
},
},
overrides={
"fcm": {
"data": {
"key": "value",
},
},
},
),
novu_py.TriggerEventRequestDto(
workflow_id="workflow_identifier",
to=[
{
"topic_key": "<value>",
"type": novu_py.TriggerRecipientsTypeEnum.SUBSCRIBER,
},
],
payload={
"comment_id": "string",
"post": {
"text": "string",
},
},
overrides={
"fcm": {
"data": {
"key": "value",
},
},
},
),
novu_py.TriggerEventRequestDto(
workflow_id="workflow_identifier",
to=[
"SUBSCRIBER_ID",
"SUBSCRIBER_ID",
],
payload={
"comment_id": "string",
"post": {
"text": "string",
},
},
overrides={
"fcm": {
"data": {
"key": "value",
},
},
},
),
],
})
# Handle response
print(res)
The same SDK client can also be used to make asychronous requests by importing asyncio.
# Asynchronous Example
import asyncio
import novu_py
from novu_py import Novu
import os
async def main():
async with Novu(
secret_key=os.getenv("NOVU_SECRET_KEY", ""),
) as novu:
res = await novu.trigger_bulk_async(bulk_trigger_event_dto={
"events": [
novu_py.TriggerEventRequestDto(
workflow_id="workflow_identifier",
to={
"subscriber_id": "<id>",
},
payload={
"comment_id": "string",
"post": {
"text": "string",
},
},
overrides={
"fcm": {
"data": {
"key": "value",
},
},
},
),
novu_py.TriggerEventRequestDto(
workflow_id="workflow_identifier",
to=[
{
"topic_key": "<value>",
"type": novu_py.TriggerRecipientsTypeEnum.SUBSCRIBER,
},
],
payload={
"comment_id": "string",
"post": {
"text": "string",
},
},
overrides={
"fcm": {
"data": {
"key": "value",
},
},
},
),
novu_py.TriggerEventRequestDto(
workflow_id="workflow_identifier",
to=[
"SUBSCRIBER_ID",
"SUBSCRIBER_ID",
],
payload={
"comment_id": "string",
"post": {
"text": "string",
},
},
overrides={
"fcm": {
"data": {
"key": "value",
},
},
},
),
],
})
# Handle response
print(res)
asyncio.run(main())
Broadcast Event to All
# Synchronous Example
from novu_py import Novu
import os
with Novu(
secret_key=os.getenv("NOVU_SECRET_KEY", ""),
) as novu:
res = novu.trigger_broadcast(trigger_event_to_all_request_dto={
"name": "<value>",
"payload": {
"comment_id": "string",
"post": {
"text": "string",
},
},
})
# Handle response
print(res)
The same SDK client can also be used to make asychronous requests by importing asyncio.
# Asynchronous Example
import asyncio
from novu_py import Novu
import os
async def main():
async with Novu(
secret_key=os.getenv("NOVU_SECRET_KEY", ""),
) as novu:
res = await novu.trigger_broadcast_async(trigger_event_to_all_request_dto={
"name": "<value>",
"payload": {
"comment_id": "string",
"post": {
"text": "string",
},
},
})
# Handle response
print(res)
asyncio.run(main())
Cancel Triggered Event
# Synchronous Example
from novu_py import Novu
import os
with Novu(
secret_key=os.getenv("NOVU_SECRET_KEY", ""),
) as novu:
res = novu.cancel(transaction_id="<id>")
# Handle response
print(res)
The same SDK client can also be used to make asychronous requests by importing asyncio.
# Asynchronous Example
import asyncio
from novu_py import Novu
import os
async def main():
async with Novu(
secret_key=os.getenv("NOVU_SECRET_KEY", ""),
) as novu:
res = await novu.cancel_async(transaction_id="<id>")
# Handle response
print(res)
asyncio.run(main())
Available Resources and Operations
Available methods
integrations
- list - Get integrations
- create - Create integration
- list_active - Get active integrations
- update - Update integration
- delete - Delete integration
- set_as_primary - Set integration as primary
integrations.webhooks
- retrieve - Get webhook support status for provider
messages
- retrieve - Get messages
- delete - Delete message
- delete_by_transaction_id - Delete messages by transactionId
notifications
notifications.stats
Novu SDK
- trigger - Trigger event
- trigger_bulk - Bulk trigger event
- trigger_broadcast - Broadcast event to all
- cancel - Cancel triggered event
subscribers
- list - Get subscribers
- create - Create subscriber
- retrieve_legacy - Get subscriber
- update - Update subscriber
delete_legacy- Delete subscriber :warning: Deprecated- create_bulk - Bulk create subscribers
- search - Search for subscribers
- retrieve - Get subscriber
- patch - Patch subscriber
- delete - Delete subscriber
subscribers.authentication
- chat_access_oauth_call_back - Handle providers oauth redirect
- chat_access_oauth - Handle chat oauth
subscribers.credentials
- update - Update subscriber credentials
- append - Modify subscriber credentials
- delete - Delete subscriber credentials by providerId
subscribers.messages
- mark_all_as - Mark a subscriber messages as seen, read, unseen or unread
- mark_all - Marks all the subscriber messages as read, unread, seen or unseen. Optionally you can pass feed id (or array) to mark messages of a particular feed.
- update_as_seen - Mark message action as seen
subscribers.notifications
- feed - Get in-app notification feed for a particular subscriber
- unseen_count - Get the unseen in-app notifications count for subscribers feed
subscribers.preferences
- list - Get subscriber preferences
- update_global - Update subscriber global preferences
- retrieve_by_level - Get subscriber preferences by level
- update_legacy - Update subscriber preference
- retrieve - Get subscriber preferences
- update - Update subscriber global or workflow specific preferences
subscribers.properties
- update_online_flag - Update subscriber online status
topics
- create - Topic creation
- list - Get topic list filtered
- delete - Delete topic
- retrieve - Get topic
- rename - Rename a topic
topics.subscribers
Pagination
Some of the endpoints in this SDK support pagination. To use pagination, you make your SDK calls as usual, but the
returned response object will have a Next method that can be called to pull down the next group of results. If the
return value of Next is None, then there are no more pages to be fetched.
Here's an example of one such pagination call:
from novu_py import Novu
import os
with Novu(
secret_key=os.getenv("NOVU_SECRET_KEY", ""),
) as novu:
res = novu.subscribers.list()
while res is not None:
# Handle items
res = res.next()
Retries
Some of the endpoints in this SDK support retries. If you use the SDK without any configuration, it will fall back to the default retry strategy provided by the API. However, the default retry strategy can be overridden on a per-operation basis, or across the entire SDK.
To change the default retry strategy for a single API call, simply provide a RetryConfig object to the call:
import novu_py
from novu_py import Novu
from novu_py.utils import BackoffStrategy, RetryConfig
import os
with Novu(
secret_key=os.getenv("NOVU_SECRET_KEY", ""),
) as novu:
res = novu.trigger(trigger_event_request_dto=novu_py.TriggerEventRequestDto(
workflow_id="workflow_identifier",
to={
"subscriber_id": "<id>",
},
payload={
"comment_id": "string",
"post": {
"text": "string",
},
},
overrides={
"fcm": {
"data": {
"key": "value",
},
},
},
),
RetryConfig("backoff", BackoffStrategy(1, 50, 1.1, 100), False))
# Handle response
print(res)
If you'd like to override the default retry strategy for all operations that support retries, you can use the retry_config optional parameter when initializing the SDK:
import novu_py
from novu_py import Novu
from novu_py.utils import BackoffStrategy, RetryConfig
import os
with Novu(
retry_config=RetryConfig("backoff", BackoffStrategy(1, 50, 1.1, 100), False),
secret_key=os.getenv("NOVU_SECRET_KEY", ""),
) as novu:
res = novu.trigger(trigger_event_request_dto=novu_py.TriggerEventRequestDto(
workflow_id="workflow_identifier",
to={
"subscriber_id": "<id>",
},
payload={
"comment_id": "string",
"post": {
"text": "string",
},
},
overrides={
"fcm": {
"data": {
"key": "value",
},
},
},
))
# Handle response
print(res)
Error Handling
Handling errors in this SDK should largely match your expectations. All operations return a response object or raise an exception.
By default, an API error will raise a models.APIError exception, which has the following properties:
| Property | Type | Description |
|---|---|---|
.status_code |
int | The HTTP status code |
.message |
str | The error message |
.raw_response |
httpx.Response | The raw HTTP response |
.body |
str | The response content |
When custom error responses are specified for an operation, the SDK may also raise their associated exceptions. You can refer to respective Errors tables in SDK docs for more details on possible exception types for each operation. For example, the trigger_async method may raise the following exceptions:
| Error Type | Status Code | Content Type |
|---|---|---|
| models.ErrorDto | 414 | application/json |
| models.ErrorDto | 400, 401, 403, 404, 405, 409, 413, 415 | application/json |
| models.ValidationErrorDto | 422 | application/json |
| models.ErrorDto | 500 | application/json |
| models.APIError | 4XX, 5XX | */* |
Example
import novu_py
from novu_py import Novu, models
import os
with Novu(
secret_key=os.getenv("NOVU_SECRET_KEY", ""),
) as novu:
res = None
try:
res = novu.trigger(trigger_event_request_dto=novu_py.TriggerEventRequestDto(
workflow_id="workflow_identifier",
to={
"subscriber_id": "<id>",
},
payload={
"comment_id": "string",
"post": {
"text": "string",
},
},
overrides={
"fcm": {
"data": {
"key": "value",
},
},
},
))
# Handle response
print(res)
except models.ErrorDto as e:
# handle e.data: models.ErrorDtoData
raise(e)
except models.ErrorDto as e:
# handle e.data: models.ErrorDtoData
raise(e)
except models.ValidationErrorDto as e:
# handle e.data: models.ValidationErrorDtoData
raise(e)
except models.ErrorDto as e:
# handle e.data: models.ErrorDtoData
raise(e)
except models.APIError as e:
# handle exception
raise(e)
Server Selection
Select Server by Index
You can override the default server globally by passing a server index to the server_idx: int optional parameter when initializing the SDK client instance. The selected server will then be used as the default on the operations that use it. This table lists the indexes associated with the available servers:
| # | Server |
|---|---|
| 0 | https://api.novu.co |
| 1 | https://eu.api.novu.co |
Example
import novu_py
from novu_py import Novu
import os
with Novu(
server_idx=1,
secret_key=os.getenv("NOVU_SECRET_KEY", ""),
) as novu:
res = novu.trigger(trigger_event_request_dto=novu_py.TriggerEventRequestDto(
workflow_id="workflow_identifier",
to={
"subscriber_id": "<id>",
},
payload={
"comment_id": "string",
"post": {
"text": "string",
},
},
overrides={
"fcm": {
"data": {
"key": "value",
},
},
},
))
# Handle response
print(res)
Override Server URL Per-Client
The default server can also be overridden globally by passing a URL to the server_url: str optional parameter when initializing the SDK client instance. For example:
import novu_py
from novu_py import Novu
import os
with Novu(
server_url="https://api.novu.co",
secret_key=os.getenv("NOVU_SECRET_KEY", ""),
) as novu:
res = novu.trigger(trigger_event_request_dto=novu_py.TriggerEventRequestDto(
workflow_id="workflow_identifier",
to={
"subscriber_id": "<id>",
},
payload={
"comment_id": "string",
"post": {
"text": "string",
},
},
overrides={
"fcm": {
"data": {
"key": "value",
},
},
},
))
# Handle response
print(res)
Custom HTTP Client
The Python SDK makes API calls using the httpx HTTP library. In order to provide a convenient way to configure timeouts, cookies, proxies, custom headers, and other low-level configuration, you can initialize the SDK client with your own HTTP client instance.
Depending on whether you are using the sync or async version of the SDK, you can pass an instance of HttpClient or AsyncHttpClient respectively, which are Protocol's ensuring that the client has the necessary methods to make API calls.
This allows you to wrap the client with your own custom logic, such as adding custom headers, logging, or error handling, or you can just pass an instance of httpx.Client or httpx.AsyncClient directly.
For example, you could specify a header for every request that this sdk makes as follows:
from novu_py import Novu
import httpx
http_client = httpx.Client(headers={"x-custom-header": "someValue"})
s = Novu(client=http_client)
or you could wrap the client with your own custom logic:
from novu_py import Novu
from novu_py.httpclient import AsyncHttpClient
import httpx
class CustomClient(AsyncHttpClient):
client: AsyncHttpClient
def __init__(self, client: AsyncHttpClient):
self.client = client
async def send(
self,
request: httpx.Request,
*,
stream: bool = False,
auth: Union[
httpx._types.AuthTypes, httpx._client.UseClientDefault, None
] = httpx.USE_CLIENT_DEFAULT,
follow_redirects: Union[
bool, httpx._client.UseClientDefault
] = httpx.USE_CLIENT_DEFAULT,
) -> httpx.Response:
request.headers["Client-Level-Header"] = "added by client"
return await self.client.send(
request, stream=stream, auth=auth, follow_redirects=follow_redirects
)
def build_request(
self,
method: str,
url: httpx._types.URLTypes,
*,
content: Optional[httpx._types.RequestContent] = None,
data: Optional[httpx._types.RequestData] = None,
files: Optional[httpx._types.RequestFiles] = None,
json: Optional[Any] = None,
params: Optional[httpx._types.QueryParamTypes] = None,
headers: Optional[httpx._types.HeaderTypes] = None,
cookies: Optional[httpx._types.CookieTypes] = None,
timeout: Union[
httpx._types.TimeoutTypes, httpx._client.UseClientDefault
] = httpx.USE_CLIENT_DEFAULT,
extensions: Optional[httpx._types.RequestExtensions] = None,
) -> httpx.Request:
return self.client.build_request(
method,
url,
content=content,
data=data,
files=files,
json=json,
params=params,
headers=headers,
cookies=cookies,
timeout=timeout,
extensions=extensions,
)
s = Novu(async_client=CustomClient(httpx.AsyncClient()))
Authentication
Per-Client Security Schemes
This SDK supports the following security scheme globally:
| Name | Type | Scheme | Environment Variable |
|---|---|---|---|
secret_key |
apiKey | API key | NOVU_SECRET_KEY |
To authenticate with the API the secret_key parameter must be set when initializing the SDK client instance. For example:
import novu_py
from novu_py import Novu
import os
with Novu(
secret_key=os.getenv("NOVU_SECRET_KEY", ""),
) as novu:
res = novu.trigger(trigger_event_request_dto=novu_py.TriggerEventRequestDto(
workflow_id="workflow_identifier",
to={
"subscriber_id": "<id>",
},
payload={
"comment_id": "string",
"post": {
"text": "string",
},
},
overrides={
"fcm": {
"data": {
"key": "value",
},
},
},
))
# Handle response
print(res)
Resource Management
The Novu class implements the context manager protocol and registers a finalizer function to close the underlying sync and async HTTPX clients it uses under the hood. This will close HTTP connections, release memory and free up other resources held by the SDK. In short-lived Python programs and notebooks that make a few SDK method calls, resource management may not be a concern. However, in longer-lived programs, it is beneficial to create a single SDK instance via a context manager and reuse it across the application.
from novu_py import Novu
import os
def main():
with Novu(
secret_key=os.getenv("NOVU_SECRET_KEY", ""),
) as novu:
# Rest of application here...
# Or when using async:
async def amain():
async with Novu(
secret_key=os.getenv("NOVU_SECRET_KEY", ""),
) as novu:
# Rest of application here...
Debugging
You can setup your SDK to emit debug logs for SDK requests and responses.
You can pass your own logger class directly into your SDK.
from novu_py import Novu
import logging
logging.basicConfig(level=logging.DEBUG)
s = Novu(debug_logger=logging.getLogger("novu_py"))
You can also enable a default debug logger by setting an environment variable NOVU_DEBUG to true.
Development
Maturity
This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.
Contributions
While we value open-source contributions to this SDK, this library is generated programmatically. Any manual changes added to internal files will be overwritten on the next generation. We look forward to hearing your feedback. Feel free to open a PR or an issue with a proof of concept and we'll do our best to include it in a future release.
SDK Created by Speakeasy
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file novu_py-0.3.2.tar.gz.
File metadata
- Download URL: novu_py-0.3.2.tar.gz
- Upload date:
- Size: 98.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.1 CPython/3.9.21 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a21906ab4581e81d4c311656e091d2d46f9d99db1b71435ee76f87c08b5c6036
|
|
| MD5 |
88a911a2c7b9de15ac9a67f80a2aa5aa
|
|
| BLAKE2b-256 |
f796f21a09fa46fe95b842955725c8856b4ecfc1c6150a06997aa4f665eab8f3
|
File details
Details for the file novu_py-0.3.2-py3-none-any.whl.
File metadata
- Download URL: novu_py-0.3.2-py3-none-any.whl
- Upload date:
- Size: 210.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.1 CPython/3.9.21 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e568dd87d49817191409960601d7a6c07293abf6f245c7afb6e9220c7dfb24b1
|
|
| MD5 |
b705e193a5738a57e46450b840682835
|
|
| BLAKE2b-256 |
b0618938fb5e168344321f350d18b2261b8b366a7683f2059d1bd8b41f8648a6
|