Skip to main content
POST
/
v2
/
topics
/
{topicKey}
/
subscriptions
PHP
declare(strict_types=1);

require 'vendor/autoload.php';

use novu;
use novu\Models\Components;

$sdk = novu\Novu::builder()
    ->setSecurity(
        'YOUR_SECRET_KEY_HERE'
    )
    ->build();

$createTopicSubscriptionsRequestDto = new Components\CreateTopicSubscriptionsRequestDto(
    subscriptions: [
        new Components\TopicSubscriberIdentifierDto(
            identifier: 'subscriber-123-subscription-a',
            subscriberId: 'subscriber-123',
        ),
        new Components\TopicSubscriberIdentifierDto(
            identifier: 'subscriber-456-subscription-b',
            subscriberId: 'subscriber-456',
        ),
    ],
    name: 'My Topic',
    context: [
        'key' => 'org-acme',
    ],
    preferences: [
        new Components\WorkflowPreferenceRequestDto(
            condition: [
                '===' => [
                    [
                        'var' => 'tier',
                    ],
                    'premium',
                ],
            ],
            workflowId: 'workflow-123',
        ),
    ],
);

$response = $sdk->topics->subscriptions->create(
    topicKey: '<value>',
    createTopicSubscriptionsRequestDto: $createTopicSubscriptionsRequestDto

);

if ($response->createSubscriptionsResponseDto !== null) {
    // handle response
}
{
  "data": [
    {
      "_id": "64f5e95d3d7946d80d0cb679",
      "topic": {
        "_id": "64f5e95d3d7946d80d0cb677",
        "key": "product-updates",
        "name": "Product Updates"
      },
      "subscriber": {
        "_id": "64da692e9a94fb2e6449ad07",
        "subscriberId": "user-123",
        "avatar": "https://example.com/avatar.png",
        "firstName": "John",
        "lastName": "Doe",
        "email": "john@example.com"
      },
      "createdAt": "2025-04-24T05:40:21Z",
      "updatedAt": "2025-04-24T05:40:21Z",
      "identifier": "tk=product-updates:si=subscriber-123",
      "name": "My Subscription",
      "preferences": [
        {
          "subscriptionId": "64f5e95d3d7946d80d0cb679",
          "enabled": true,
          "workflow": {
            "id": "64a1b2c3d4e5f6g7h8i9j0k1",
            "identifier": "welcome-email",
            "name": "Welcome Email Workflow",
            "critical": false,
            "tags": [
              "user-onboarding",
              "email"
            ],
            "data": {
              "category": "onboarding",
              "priority": "high"
            }
          },
          "condition": {
            "and": [
              {
                "===": [
                  {
                    "var": "tier"
                  },
                  "premium"
                ]
              }
            ]
          }
        }
      ],
      "contextKeys": [
        "tenant:org-a",
        "project:proj-123"
      ]
    }
  ],
  "meta": {
    "totalCount": 3,
    "successful": 2,
    "failed": 1
  },
  "errors": [
    {
      "subscriberId": "invalid-subscriber-id",
      "code": "SUBSCRIBER_NOT_FOUND",
      "message": "Subscriber with ID invalid-subscriber-id could not be found"
    }
  ]
}

Authorizations

Authorization
string
header
required

API key authentication. Allowed headers-- "Authorization: ApiKey <novu_secret_key>".

Headers

idempotency-key
string

A header for idempotency purposes

Path Parameters

topicKey
string
required

The key identifier of the topic

Body

application/json
subscriberIds
string[]
deprecated

List of subscriber IDs to subscribe to the topic (max: 100). @deprecated Use the "subscriptions" property instead.

Example:
["subscriberId1", "subscriberId2"]
subscriptions
(string | object)[]

List of subscriptions to subscribe to the topic (max: 100). Can be either a string array of subscriber IDs or an array of objects with identifier and subscriberId

Example:
[
{
"identifier": "subscriber-123-subscription-a",
"subscriberId": "subscriber-123"
},
{
"identifier": "subscriber-456-subscription-b",
"subscriberId": "subscriber-456"
}
]
name
string

The name of the topic

Example:

"My Topic"

context
object
preferences
string · object · object[]

The preferences of the topic. Can be a simple workflow ID string, workflow preference object, or group filter object

Example:
[
{
"workflowId": "workflow-123",
"condition": { "===": [{ "var": "tier" }, "premium"] }
}
]

Response

Subscriptions created successfully

data
object[]
required

The list of successfully created subscriptions

meta
object
required

Metadata about the operation

errors
object[]

The list of errors for failed subscription attempts