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
Tip
To finish publishing your SDK you must run your first generation action.
The SDK relies on Composer to manage its dependencies.
To install the SDK first add the below to your composer.json
file:
{
"repositories": [
{
"type": "github",
"url": "<UNSET>.git"
}
],
"require": {
"novuhq/novu": "*"
}
}
Then run the following command:
composer update
declare(strict_types=1);
require 'vendor/autoload.php';
use novu;
use novu\Models\Components;
$sdk = novu\Novu::builder()
->setSecurity(
'<YOUR_API_KEY_HERE>'
)
->build();
$request = new Components\TriggerEventRequestDto(
name: 'workflow_identifier',
to: new Components\SubscriberPayloadDto(
subscriberId: '<id>',
),
payload: [
'comment_id' => 'string',
'post' => [
'text' => 'string',
],
],
bridgeUrl: 'https://example.com/bridge',
overrides: [
'fcm' => [
'data' => [
'key' => 'value',
],
],
],
);
$response = $sdk->trigger(
request: $request
);
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_API_KEY_HERE>'
)
->build();
$request = new Components\BulkTriggerEventDto(
events: [
new Components\TriggerEventRequestDto(
name: 'workflow_identifier',
to: new Components\TopicPayloadDto(
topicKey: '<value>',
type: Components\TriggerRecipientsTypeEnum::Topic,
),
payload: [
'comment_id' => 'string',
'post' => [
'text' => 'string',
],
],
bridgeUrl: 'https://example.com/bridge',
overrides: [
'fcm' => [
'data' => [
'key' => 'value',
],
],
],
),
],
);
$response = $sdk->triggerBulk(
request: $request
);
if ($response->triggerEventResponseDtos !== null) {
// handle response
}
declare(strict_types=1);
require 'vendor/autoload.php';
use novu;
use novu\Models\Components;
$sdk = novu\Novu::builder()
->setSecurity(
'<YOUR_API_KEY_HERE>'
)
->build();
$request = new Components\TriggerEventToAllRequestDto(
name: '<value>',
payload: [
'comment_id' => 'string',
'post' => [
'text' => 'string',
],
],
overrides: new Components\Overrides(),
);
$response = $sdk->broadcast(
request: $request
);
if ($response->triggerEventResponseDto !== null) {
// handle response
}
declare(strict_types=1);
require 'vendor/autoload.php';
use novu;
$sdk = novu\Novu::builder()
->setSecurity(
'<YOUR_API_KEY_HERE>'
)
->build();
$response = $sdk->cancel(
transactionId: '<id>'
);
if ($response->dataBooleanDto !== null) {
// handle response
}
This SDK supports the following security scheme globally:
Name | Type | Scheme |
---|---|---|
apiKey |
apiKey | API key |
To authenticate with the API the apiKey
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_API_KEY_HERE>'
)
->build();
$request = new Components\TriggerEventToAllRequestDto(
name: '<value>',
payload: [
'comment_id' => 'string',
'post' => [
'text' => 'string',
],
],
overrides: new Components\Overrides(),
);
$response = $sdk->broadcast(
request: $request
);
if ($response->triggerEventResponseDto !== null) {
// handle response
}
Available methods
- create - Create integration
- listActive - Get active integrations
- list - Get integrations
- delete - Delete integration
- setPrimary - Set integration as primary
- update - Update integration
- getProviderStatus - Get webhook support status for provider
- delete - Delete message
- deleteByTransactionId - Delete messages by transactionId
- list - Get messages
- broadcast - Broadcast event to all
- cancel - Cancel triggered event
- trigger - Trigger event
- triggerBulk - Bulk trigger event
- createBulk - Bulk create subscribers
- create - Create subscriber
- get - Get subscriber
- list - Get subscribers
- delete - Delete subscriber
- update - Update subscriber
- updateOnlineStatus - Update subscriber online status
- chatAccessOauth - Handle chat oauth
- oauthCallback - Handle providers oauth redirect
- delete - Delete subscriber credentials by providerId
- append - Modify subscriber credentials
- update - Update subscriber credentials
- updateAction - Mark message action as seen
- markAll - 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.
- markAs - Mark a subscriber messages as seen, read, unseen or unread
- getFeed - Get in-app notification feed for a particular subscriber
- unseenCount - Get the unseen in-app notifications count for subscribers feed
- retrieveByLevel - Get subscriber preferences by level
- list - Get subscriber preferences
- updateGlobal - Update subscriber global preferences
- update - Update subscriber preference
- create - Topic creation
- delete - Delete topic
- get - Get topic
- list - Get topic list filtered
- rename - Rename a topic
- add - Subscribers addition
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_API_KEY_HERE>'
)
->build();
$responses = $sdk->subscribers->list(
page: 7685.78,
limit: 10
);
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_API_KEY_HERE>'
)
->build();
$request = new Components\TriggerEventToAllRequestDto(
name: '<value>',
payload: [
'comment_id' => 'string',
'post' => [
'text' => 'string',
],
],
overrides: new Components\Overrides(),
);
$response = $sdk->broadcast(
request: $request,
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_API_KEY_HERE>'
)
->build();
$request = new Components\TriggerEventToAllRequestDto(
name: '<value>',
payload: [
'comment_id' => 'string',
'post' => [
'text' => 'string',
],
],
overrides: new Components\Overrides(),
);
$response = $sdk->broadcast(
request: $request
);
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 broadcast
method throws the following exceptions:
Error Type | Status Code | Content Type |
---|---|---|
Errors\ErrorDto | 400, 404, 409 | application/json |
Errors\ValidationErrorDto | 422 | application/json |
Errors\APIException | 4XX, 5XX | */* |
declare(strict_types=1);
require 'vendor/autoload.php';
use novu;
use novu\Models\Components;
$sdk = novu\Novu::builder()
->setSecurity(
'<YOUR_API_KEY_HERE>'
)
->build();
try {
$request = new Components\TriggerEventToAllRequestDto(
name: '<value>',
payload: [
'comment_id' => 'string',
'post' => [
'text' => 'string',
],
],
overrides: new Components\Overrides(),
);
$response = $sdk->broadcast(
request: $request
);
if ($response->triggerEventResponseDto !== null) {
// handle response
}
} catch (Errors\ErrorDtoThrowable $e) {
// handle $e->$container data
throw $e;
} catch (Errors\ValidationErrorDtoThrowable $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 |
---|---|
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_API_KEY_HERE>'
)
->build();
$request = new Components\TriggerEventToAllRequestDto(
name: '<value>',
payload: [
'comment_id' => 'string',
'post' => [
'text' => 'string',
],
],
overrides: new Components\Overrides(),
);
$response = $sdk->broadcast(
request: $request
);
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://api.novu.co')
->setSecurity(
'<YOUR_API_KEY_HERE>'
)
->build();
$request = new Components\TriggerEventToAllRequestDto(
name: '<value>',
payload: [
'comment_id' => 'string',
'post' => [
'text' => 'string',
],
],
overrides: new Components\Overrides(),
);
$response = $sdk->broadcast(
request: $request
);
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.