The PHP Novu SDK and package provides a fluent and expressive interface for interacting with Novu's API and managing notifications.
Developer-friendly & type-safe Php SDK specifically catered to leverage novuhq/novu API.
Important
This SDK is not yet ready for production use. To complete setup please follow the steps outlined in your workspace. Delete this section before > publishing to a package manager.
Novu API: Novu REST API. Please see https://docs.novu.co/api-reference for more details.
For more information about the API: Novu Documentation
The SDK relies on Composer to manage its dependencies.
To install the SDK and add it as a dependency to an existing composer.json
file:
composer require "novuhq/novu"
declare(strict_types=1);
require 'vendor/autoload.php';
use novu;
use novu\Models\Components;
$sdk = novu\Novu::builder()
->setSecurity(
'YOUR_SECRET_KEY_HERE'
)
->build();
$triggerEventRequestDto = new Components\TriggerEventRequestDto(
workflowId: 'workflow_identifier',
payload: [
'comment_id' => 'string',
'post' => [
'text' => 'string',
],
],
overrides: new Components\Overrides(),
to: new Components\SubscriberPayloadDto(
subscriberId: '<id>',
),
);
$response = $sdk->trigger(
triggerEventRequestDto: $triggerEventRequestDto,
idempotencyKey: '<value>'
);
if ($response->triggerEventResponseDto !== null) {
// handle response
}
declare(strict_types=1);
require 'vendor/autoload.php';
use novu;
$sdk = novu\Novu::builder()
->setSecurity(
'YOUR_SECRET_KEY_HERE'
)
->build();
$response = $sdk->cancel(
transactionId: '<id>',
idempotencyKey: '<value>'
);
if ($response->boolean !== null) {
// handle response
}
declare(strict_types=1);
require 'vendor/autoload.php';
use novu;
use novu\Models\Components;
$sdk = novu\Novu::builder()
->setSecurity(
'YOUR_SECRET_KEY_HERE'
)
->build();
$triggerEventToAllRequestDto = new Components\TriggerEventToAllRequestDto(
name: '<value>',
payload: [
'comment_id' => 'string',
'post' => [
'text' => 'string',
],
],
overrides: new Components\TriggerEventToAllRequestDtoOverrides(
additionalProperties: [
'fcm' => [
'data' => [
'key' => 'value',
],
],
],
),
);
$response = $sdk->triggerBroadcast(
triggerEventToAllRequestDto: $triggerEventToAllRequestDto,
idempotencyKey: '<value>'
);
if ($response->triggerEventResponseDto !== null) {
// handle response
}
declare(strict_types=1);
require 'vendor/autoload.php';
use novu;
use novu\Models\Components;
$sdk = novu\Novu::builder()
->setSecurity(
'YOUR_SECRET_KEY_HERE'
)
->build();
$bulkTriggerEventDto = new Components\BulkTriggerEventDto(
events: [
new Components\TriggerEventRequestDto(
workflowId: 'workflow_identifier',
payload: [
'comment_id' => 'string',
'post' => [
'text' => 'string',
],
],
overrides: new Components\Overrides(),
to: new Components\TopicPayloadDto(
topicKey: '<value>',
type: Components\TriggerRecipientsTypeEnum::Topic,
),
),
],
);
$response = $sdk->triggerBulk(
bulkTriggerEventDto: $bulkTriggerEventDto,
idempotencyKey: '<value>'
);
if ($response->triggerEventResponseDtos !== null) {
// handle response
}
This SDK supports the following security scheme globally:
Name | Type | Scheme |
---|---|---|
secretKey |
apiKey | API key |
To authenticate with the API the secretKey
parameter must be set when initializing the SDK. For example:
declare(strict_types=1);
require 'vendor/autoload.php';
use novu;
use novu\Models\Components;
$sdk = novu\Novu::builder()
->setSecurity(
'YOUR_SECRET_KEY_HERE'
)
->build();
$triggerEventRequestDto = new Components\TriggerEventRequestDto(
workflowId: 'workflow_identifier',
payload: [
'comment_id' => 'string',
'post' => [
'text' => 'string',
],
],
overrides: new Components\Overrides(),
to: new Components\SubscriberPayloadDto(
subscriberId: '<id>',
),
);
$response = $sdk->trigger(
triggerEventRequestDto: $triggerEventRequestDto,
idempotencyKey: '<value>'
);
if ($response->triggerEventResponseDto !== null) {
// handle response
}
Available methods
- list - Get integrations
- create - Create integration
- update - Update integration
- delete - Delete integration
- setAsPrimary - Set integration as primary
- listActive - Get active integrations
- getSupportStatus - Get webhook support status for provider
- get - Get messages
- delete - Delete message
- deleteByTransactionId - Delete messages by transactionId
- get - Get notification statistics
- getGraph - Get notification graph statistics
- trigger - Trigger event
- cancel - Cancel triggered event
- triggerBroadcast - Broadcast event to all
- triggerBulk - Bulk trigger event
- search - Search for subscribers
- create - Create subscriber
- get - Get subscriber
- patch - Patch subscriber
- delete - Delete subscriber
- list - Get subscribers
- update - Upsert subscriber
- createBulk - Bulk create subscribers
- updatePreferences - Update subscriber global or workflow specific preferences
- updateCredentials - Update subscriber credentials
- updateOnlineStatus - Update subscriber online status
- handleOauthCallback - Handle providers oauth redirect
- updateAsSeen - Mark message action as seen
- markAllAs - Mark a subscriber messages as seen, read, unseen or unread
- getFeed - Get in-app notification feed for a particular subscriber
- list - List topics a subscriber is subscribed to
- chatAccessOauth - Handle chat oauth
- append - Modify subscriber credentials
- deleteProvider - Delete subscriber credentials by providerId
- markAll - Marks all the subscriber messages as read, unread, seen or unseen.
- getUnseenCount - Get the unseen in-app notifications count for subscribers feed
- list - Get subscriber preferences
- list - Get topics list
- create - Create or update a topic
- get - Get topic by key
- update - Update topic by key
- delete - Delete topic by key
- list - List topic subscriptions
- create - Create topic subscriptions, if the topic does not exist, it will be created.
- delete - Delete topic subscriptions
- check - Check topic subscriber
Some of the endpoints in this SDK support pagination. To use pagination, you make your SDK calls as usual, but the
returned object will be a Generator
instead of an individual response.
Working with generators is as simple as iterating over the responses in a foreach
loop, and you can see an example below:
declare(strict_types=1);
require 'vendor/autoload.php';
use novu;
$sdk = novu\Novu::builder()
->setSecurity(
'YOUR_SECRET_KEY_HERE'
)
->build();
$responses = $sdk->subscribers->list(
page: 7685.78,
limit: 10,
idempotencyKey: '<value>'
);
foreach ($responses as $response) {
if ($response->statusCode === 200) {
// handle response
}
}
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 an Options
object built with a RetryConfig
object to the call:
declare(strict_types=1);
require 'vendor/autoload.php';
use novu;
use novu\Models\Components;
use novu\Utils\Retry;
$sdk = novu\Novu::builder()
->setSecurity(
'YOUR_SECRET_KEY_HERE'
)
->build();
$triggerEventRequestDto = new Components\TriggerEventRequestDto(
workflowId: 'workflow_identifier',
payload: [
'comment_id' => 'string',
'post' => [
'text' => 'string',
],
],
overrides: new Components\Overrides(),
to: new Components\SubscriberPayloadDto(
subscriberId: '<id>',
),
);
$response = $sdk->trigger(
triggerEventRequestDto: $triggerEventRequestDto,
idempotencyKey: '<value>',
options: Utils\Options->builder()->setRetryConfig(
new Retry\RetryConfigBackoff(
initialInterval: 1,
maxInterval: 50,
exponent: 1.1,
maxElapsedTime: 100,
retryConnectionErrors: false,
))->build()
);
if ($response->triggerEventResponseDto !== null) {
// handle response
}
If you'd like to override the default retry strategy for all operations that support retries, you can pass a RetryConfig
object to the SDKBuilder->setRetryConfig
function when initializing the SDK:
declare(strict_types=1);
require 'vendor/autoload.php';
use novu;
use novu\Models\Components;
use novu\Utils\Retry;
$sdk = novu\Novu::builder()
->setRetryConfig(
new Retry\RetryConfigBackoff(
initialInterval: 1,
maxInterval: 50,
exponent: 1.1,
maxElapsedTime: 100,
retryConnectionErrors: false,
)
)
->setSecurity(
'YOUR_SECRET_KEY_HERE'
)
->build();
$triggerEventRequestDto = new Components\TriggerEventRequestDto(
workflowId: 'workflow_identifier',
payload: [
'comment_id' => 'string',
'post' => [
'text' => 'string',
],
],
overrides: new Components\Overrides(),
to: new Components\SubscriberPayloadDto(
subscriberId: '<id>',
),
);
$response = $sdk->trigger(
triggerEventRequestDto: $triggerEventRequestDto,
idempotencyKey: '<value>'
);
if ($response->triggerEventResponseDto !== null) {
// handle response
}
Handling errors in this SDK should largely match your expectations. All operations return a response object or throw an exception.
By default an API error will raise a Errors\APIException
exception, which has the following properties:
Property | Type | Description |
---|---|---|
$message |
string | The error message |
$statusCode |
int | The HTTP status code |
$rawResponse |
?\Psr\Http\Message\ResponseInterface | The raw HTTP response |
$body |
string | The response content |
When custom error responses are specified for an operation, the SDK may also throw their associated exception. You can refer to respective Errors tables in SDK docs for more details on possible exception types for each operation. For example, the trigger
method throws the following exceptions:
Error Type | Status Code | Content Type |
---|---|---|
Errors\ErrorDto | 414 | application/json |
Errors\ErrorDto | 400, 401, 403, 404, 405, 409, 413, 415 | application/json |
Errors\ValidationErrorDto | 422 | application/json |
Errors\ErrorDto | 500 | application/json |
Errors\APIException | 4XX, 5XX | */* |
declare(strict_types=1);
require 'vendor/autoload.php';
use novu;
use novu\Models\Components;
use novu\Models\Errors;
$sdk = novu\Novu::builder()
->setSecurity(
'YOUR_SECRET_KEY_HERE'
)
->build();
try {
$triggerEventRequestDto = new Components\TriggerEventRequestDto(
workflowId: 'workflow_identifier',
payload: [
'comment_id' => 'string',
'post' => [
'text' => 'string',
],
],
overrides: new Components\Overrides(),
to: new Components\SubscriberPayloadDto(
subscriberId: '<id>',
),
);
$response = $sdk->trigger(
triggerEventRequestDto: $triggerEventRequestDto,
idempotencyKey: '<value>'
);
if ($response->triggerEventResponseDto !== null) {
// handle response
}
} catch (Errors\ErrorDtoThrowable $e) {
// handle $e->$container data
throw $e;
} catch (Errors\ErrorDtoThrowable $e) {
// handle $e->$container data
throw $e;
} catch (Errors\ValidationErrorDtoThrowable $e) {
// handle $e->$container data
throw $e;
} catch (Errors\ErrorDtoThrowable $e) {
// handle $e->$container data
throw $e;
} catch (Errors\APIException $e) {
// handle default exception
throw $e;
}
You can override the default server globally using the setServerIndex(int $serverIdx)
builder method 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 | Description |
---|---|---|
0 | https://api.novu.co |
|
1 | https://eu.api.novu.co |
declare(strict_types=1);
require 'vendor/autoload.php';
use novu;
use novu\Models\Components;
$sdk = novu\Novu::builder()
->setServerIndex(1)
->setSecurity(
'YOUR_SECRET_KEY_HERE'
)
->build();
$triggerEventRequestDto = new Components\TriggerEventRequestDto(
workflowId: 'workflow_identifier',
payload: [
'comment_id' => 'string',
'post' => [
'text' => 'string',
],
],
overrides: new Components\Overrides(),
to: new Components\SubscriberPayloadDto(
subscriberId: '<id>',
),
);
$response = $sdk->trigger(
triggerEventRequestDto: $triggerEventRequestDto,
idempotencyKey: '<value>'
);
if ($response->triggerEventResponseDto !== null) {
// handle response
}
The default server can also be overridden globally using the setServerUrl(string $serverUrl)
builder method when initializing the SDK client instance. For example:
declare(strict_types=1);
require 'vendor/autoload.php';
use novu;
use novu\Models\Components;
$sdk = novu\Novu::builder()
->setServerURL('https://eu.api.novu.co')
->setSecurity(
'YOUR_SECRET_KEY_HERE'
)
->build();
$triggerEventRequestDto = new Components\TriggerEventRequestDto(
workflowId: 'workflow_identifier',
payload: [
'comment_id' => 'string',
'post' => [
'text' => 'string',
],
],
overrides: new Components\Overrides(),
to: new Components\SubscriberPayloadDto(
subscriberId: '<id>',
),
);
$response = $sdk->trigger(
triggerEventRequestDto: $triggerEventRequestDto,
idempotencyKey: '<value>'
);
if ($response->triggerEventResponseDto !== null) {
// handle response
}
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.
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.