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: 'SUBSCRIBER_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: 'SUBSCRIBER_ID',
),
],
);
$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: 'SUBSCRIBER_ID',
);
$response = $sdk->trigger(
triggerEventRequestDto: $triggerEventRequestDto,
idempotencyKey: '<value>'
);
if ($response->triggerEventResponseDto !== null) {
// handle response
}
Available methods
- list - List all integrations
- create - Create an integration
- update - Update an integration
- delete - Delete an integration
- setAsPrimary - Update integration as primary
- listActive - List active integrations
- get - List all messages
- delete - Delete a message
- deleteByTransactionId - Delete messages by transactionId
- trigger - Trigger event
- cancel - Cancel triggered event
- triggerBroadcast - Broadcast event to all
- triggerBulk - Bulk trigger event
- search - Search subscribers
- create - Create a subscriber
- get - Retrieve a subscriber
- patch - Update a subscriber
- delete - Delete subscriber
- createBulk - Bulk create subscribers
- updatePreferences - Update subscriber preferences
- updateCredentials - Update provider credentials
- updateOnlineStatus - Update subscriber online status
- updateAsSeen - Update notification action status
- markAllAs - Update notifications state
- getFeed - Retrieve subscriber notifications
- list - Retrieve subscriber subscriptions
- append - Upsert provider credentials
- deleteProvider - Delete provider credentials
- markAll - Update all notifications state
- getUnseenCount - Retrieve unseen notifications count
- list - Retrieve subscriber preferences
- list - List all topics
- create - Create a topic
- get - Retrieve a topic
- update - Update a topic
- delete - Delete a topic
- list - List topic subscriptions
- create - Create topic subscriptions
- delete - Delete topic subscriptions
- check - Check topic subscriber
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: 'SUBSCRIBER_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: 'SUBSCRIBER_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: 'SUBSCRIBER_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: 'SUBSCRIBER_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: 'SUBSCRIBER_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.