Skip to content

Getting Started ​

This guide walks you through setting up GrydNotifications in your .NET application, from installation to sending your first notification.

Prerequisites ​

  • .NET 10.0 or later
  • PostgreSQL (for notification persistence and template storage)
  • An existing Gryd.IO project (or use the gryd-api template)
  • Only for e-mail attachments: GrydFiles (AddGrydFiles, with object storage and an antivirus engine) and the gryd.notification-attachment profile. A host that never sends an attachment — in-app only, or e-mail without files — does not register it; see Email with Attachments.

1. Installation ​

bash
dotnet add package GrydNotifications

This includes Core, Application, Infrastructure (MailKit + EF Core + Scriban), and API layers. SMTP is used only when Email.SmtpHost is configured; without it, the default sender returns a failure. Push also returns a failure until an FCM or other push sender is registered. Crash recovery for queued notifications is opt-in; see Durable delivery.

Individual packages ​

bash
dotnet add package GrydNotifications.Core
bash
dotnet add package GrydNotifications.Core
dotnet add package GrydNotifications.Application
dotnet add package GrydNotifications.Infrastructure
dotnet add package GrydNotifications.API
bash
dotnet add package GrydNotifications
dotnet add package GrydNotifications.Scheduling

2. Configure Services ​

The generated API template is the maintained host composition. It configures core services, API versioning, authentication, readiness checks, and middleware around feature registration. See its Notifications service registration and pipeline. Inside that host's service-registration section, use the current facade overload:

csharp
using GrydNotifications;

builder.Services.AddGrydNotifications(builder.Configuration);

The facade binds GrydNotifications and resolves its PostgreSQL connection in this order: GrydNotifications:ConnectionString, ConnectionStrings:GrydNotifications, ConnectionStrings:DefaultConnection, then ConnectionStrings:DefaultPostgresConnection.

3. Add Configuration ​

This configuration is read by the AddGrydNotifications(builder.Configuration) call above. Keep credentials in the host's secret provider; the password value below is a placeholder.

json
{
  "ConnectionStrings": {
    "GrydNotifications": "Host=localhost;Port=5432;Database=gryd_notifications;Username=postgres;Password=postgres"
  },
  "GrydNotifications": {
    "Email": {
      "DefaultFrom": "noreply@myapp.com",
      "SmtpHost": "smtp.example.com",
      "SmtpPort": 587,
      "SmtpUseSsl": true,
      "SmtpUsername": "<smtp username>",
      "SmtpPassword": "<secret from host provider>"
    },
    "Push": { "Enabled": false },
    "InApp": { "Enabled": true, "EnableSignalR": true }
  }
}

With a real SMTP host configured, SmtpUseSsl=true uses STARTTLS. Without a configured SMTP host, the default email sender returns a failure; Push returns a failure until the host registers a push provider. For crash recovery, follow Durable delivery; the optional scheduling package alone does not enable it.

4. Send Your First Notification ​

csharp
using GrydNotifications.Application.Features.Notifications.Commands;
using GrydNotifications.Application.Features.Email.Commands;
using GrydNotifications.Core.DTOs;
using GrydNotifications.Core.Enums;
using MediatR;
using Microsoft.Extensions.Logging;

// Order is an application-owned domain type.
public class OrderService
{
    private readonly IMediator _mediator;
    private readonly ILogger<OrderService> _logger;

    public OrderService(IMediator mediator, ILogger<OrderService> logger)
    {
        _mediator = mediator;
        _logger = logger;
    }

    public async Task OnOrderCreatedAsync(Order order, CancellationToken ct)
    {
        // Option 1: Unified pipeline (auto-resolves channel)
        var result = await _mediator.Send(new SendNotificationCommand
        {
            Channel = NotificationChannel.Email,
            Recipients = [new RecipientDto("user@example.com", "John Doe")],
            Subject = "Order Confirmed",
            HtmlBody = $"<h1>Order #{order.Id}</h1><p>Thank you, {order.CustomerName}!</p>"
        }, ct);

        if (!result.IsSuccess)
            _logger.LogWarning("Notification failed: {Error}", result.ErrorMessage);

        // Option 2: Direct email shortcut
        await _mediator.Send(new SendEmailCommand
        {
            To = ["user@example.com"],
            Cc = ["admin@myapp.com"],
            Subject = "New Order",
            HtmlBody = "<p>A new order was placed.</p>"
        }, ct);
    }
}

Via REST API ​

The Notifications API controllers require the GrydNotifications.Manage authorization policy; configure the host's authentication and authorization before exposing these endpoints. For in-process application code without that HTTP policy, use the MediatR command above.

http
POST /api/v1/notifications/send
Content-Type: application/json

{
  "channel": "Email",
  "recipients": [
    { "address": "user@example.com", "displayName": "John Doe" }
  ],
  "subject": "Order Confirmed",
  "htmlBody": "<h1>Your order has been confirmed!</h1>"
}

With the default asynchronous queue enabled, the success response is 200 OK and the notification is Queued. A queued status means accepted for processing, not provider delivery. Synchronous mode can return another status; inspect the result instead of assuming Sent.

json
{
  "notificationId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
  "status": "Queued",
  "statusDisplay": "Queued"
}

5. Using Templates ​

Create a template via API:

http
POST /api/v1/notification-templates
Content-Type: application/json

{
  "slug": "order-confirmation",
  "name": "Order Confirmation Email",
  "subjectTemplate": "Order #{{order_id}} Confirmed",
  "htmlBodyTemplate": "<h1>Hi {{customer_name}},</h1><p>Your order #{{order_id}} for {{total | math.format \"C2\"}} has been confirmed.</p>",
  "locale": "en-US"
}

Then send using the template:

csharp
await _mediator.Send(new SendNotificationCommand
{
    Channel = NotificationChannel.Email,
    Recipients = [new RecipientDto("user@example.com")],
    TemplateSlug = "order-confirmation",
    TemplateData = new Dictionary<string, object>
    {
        ["order_id"] = "ORD-12345",
        ["customer_name"] = "John",
        ["total"] = 199.99m
    }
}, ct);

Next Steps ​

Released under the MIT License.