Skip to main content

Sending Notifications

Notifications are sent using ductape.processor.notification.send(data) within the Ductape system. This method triggers push notifications, emails, or callbacks based on the provided environment, product tag, and other parameters.

await ductape.processor.notification.send(data: INotificationProcessorInput)

This processes a notification event in the specified environment and application context, passing along request input, metadata, and optionally session tracking.

Parameters

INotificationProcessorInput

FieldTypeRequiredDescription
envstringYesSlug of the environment (e.g., "dev", "prd").
product_tagstringYesUnique product identifier.
eventstringYesNotification event tag to be triggered.
inputINotificationRequestYesDetails of the notification to be sent.
retriesnumberNoNumber of retry attempts on failure.
cachestringNoCache tag to enable response caching.
sessionISessionNoEnables session-based dynamic injection into inputs.

Note: Optional fields like email, callback, or internal objects can be omitted or passed as empty {}.

INotificationRequest

Shape of the input object:

interface INotificationRequest {
slug: string;
push_notification: {
title: Record<string, unknown>;
body: Record<string, unknown>;
data: Record<string, unknown>;
device_token: string;
};
email?: {
to: string[];
subject: Record<string, unknown>;
template: Record<string, unknown>;
};
callback?: {
query?: Record<string, unknown>;
params?: Record<string, unknown>;
body?: Record<string, unknown>;
headers?: Record<string, unknown>;
};
sms?: Record<string, unknown>;
}

ISession Schema

The session field enables optional session tracking for any notification send.

interface ISession {
tag: string; // session tag
token: string; // session token (e.g. signed JWT)
}
FieldTypeRequiredDescription
tagstringYesSession tag reference (e.g., "user-sessions").
tokenstringYesEncrypted token used to resolve values.

Injecting Session Data into Input

You can inject properties from the session payload into the input object using the $Session{parent_key}{key} annotation. This resolves the value dynamically from the decrypted session object.

For example:

push_notification: {
title: { en: "Welcome back!" },
body: { en: "Good to see you, $Session{details}{firstName}!" },
data: { userId: "$Session{details}{id}" },
device_token: "$Session{details}{deviceToken}"
}

Ensure the session contains a matching parent_key and fields (e.g., id, firstName).

Returns

A Promise<unknown> — resolves with the result of the notification action. The structure varies depending on the notification definition.

Example

const res = await ductape.processor.notification.send({
env: "prd",
product_tag: "my-product",
event: "user_welcome",
input: {
slug: "welcome_notification",
push_notification: {
title: { en: "Welcome!" },
body: { en: "Thanks for signing up, $Session{details}{firstName}!" },
data: { action: "open_dashboard" },
device_token: "$Session{details}{deviceToken}"
},
email: {
to: ["user@example.com"],
subject: { en: "Welcome to our platform!" },
template: { en: "<h1>Hello!</h1><p>Thanks for joining us.</p>" }
},
callback: {
query: { userId: "$Session{details}{id}" },
body: { event: "welcome_sent" },
headers: { Authorization: "Bearer token" }
},
sms: {
firstname: { userId: "$Session{details}{firstName}" },
lastname: { event: "$Session{details}{lastName}" },
}
},
retries: 3,
cache: "notification-user-welcome",
session: {
tag: "user",
token: "eyJhbGciOi..."
}
});

See Also