Skip to content

Commit 0ec6e1c

Browse files
committed
wip: Skeleton of webhook example
1 parent 4cb1856 commit 0ec6e1c

File tree

7 files changed

+173
-0
lines changed

7 files changed

+173
-0
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
| [WebControllerApi](https://github.com/resend/resend-dotnet/tree/master/examples/WebControllerApi) | 5000 | Send email from a controller
1313
| [WebMinimalApi](https://github.com/resend/resend-dotnet/tree/master/examples/WebMinimalApi) | 5001 | Send email from a (minimal) API
1414
| [WebRazor](https://github.com/resend/resend-dotnet/tree/master/examples/WebRazor) | 5002 | Send email from a Razor form
15+
| [WebWebhook](https://github.com/resend/resend-dotnet/tree/master/examples/WebWebhook) | 5007 | Receive webhook events from Resend
1516

1617

1718
Async

examples/WebWebhook/Program.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Resend;
3+
4+
5+
/*
6+
*
7+
*/
8+
var builder = WebApplication.CreateBuilder( args );
9+
10+
builder.Services.AddResendWebhooks( o =>
11+
{
12+
o.Secret = Environment.GetEnvironmentVariable( "RESEND_WEBHOOK_SECRET" )!;
13+
} );
14+
15+
16+
builder.Services.AddOptions();
17+
builder.Services.Configure<ResendClientOptions>( o =>
18+
{
19+
o.ApiToken = Environment.GetEnvironmentVariable( "RESEND_APITOKEN" )!;
20+
} );
21+
builder.Services.AddHttpClient<ResendClient>();
22+
builder.Services.AddTransient<IResend, ResendClient>();
23+
24+
25+
/*
26+
*
27+
*/
28+
var app = builder.Build();
29+
30+
app.MapResendWebhook( "/webhook" );
31+
32+
app.MapPost( "/email/send", async ( [FromServices] IResend resend, [FromServices] ILogger<EmailSend> logger ) =>
33+
{
34+
var message = new EmailMessage();
35+
message.From = "[email protected]";
36+
message.To.Add( "[email protected]" );
37+
message.Subject = "Hello from Minimal API";
38+
message.TextBody = "Email using Resend .NET SDK";
39+
40+
var resp = await resend.EmailSendAsync( message );
41+
42+
logger.LogInformation( "Sent email, with Id = {EmailId}", resp.Content );
43+
44+
return Results.Ok();
45+
} );
46+
47+
48+
/*
49+
*
50+
*/
51+
app.Run();
52+
53+
54+
/*
55+
*
56+
*/
57+
public class EmailSend { };
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:5007",
8+
"sslPort": 0
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": false,
16+
"launchUrl": "email/send",
17+
"applicationUrl": "http://localhost:5007",
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development"
20+
}
21+
},
22+
"IIS Express": {
23+
"commandName": "IISExpress",
24+
"launchBrowser": false,
25+
"launchUrl": "email/send",
26+
"environmentVariables": {
27+
"ASPNETCORE_ENVIRONMENT": "Development"
28+
}
29+
}
30+
}
31+
}

examples/WebWebhook/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
.NET SDK: ASP.NET - Webhook
2+
=====================================================================
3+
4+
This example shows how to receive webhook events from Resend.
5+
6+
7+
How to run
8+
---------------------------------------------------------------------
9+
10+
1. Create a Webhook on the Resend dashboard
11+
- Go to the [Webhooks page](https://resend.com/webhooks)
12+
- The endpoint URL needs to be HTTPS (You can use [Ngrok](https://ngrok.com/) to point to the dotnet app)
13+
- Copy the Webhook signing secret after creating the Webhook
14+
15+
2. Set the `RESEND_WEBHOOK_SECRET` environment variable to your Webhook
16+
17+
18+
1. Set the `RESEND_APITOKEN` environment variable to your Resend API.
19+
2. Edit the `From` and `To` in the `Program.cs` as necessary.
20+
3. Run the console app with `dotnet run`.
21+
4. Make a `POST` request to `http://localhost:5007/email/send`
22+
23+
```bash
24+
set RESEND_WEBHOOK_SECRET=whsec_alJairX6NuQxWqjdF8PIQd48gh2IQOHl
25+
set RESEND_APITOKEN=re_8m9gwsVG_6n94KaJkJ42Yj6qSeVvLq9xF
26+
dotnet run
27+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\..\src\Resend.Webhooks\Resend.Webhooks.csproj" />
11+
<ProjectReference Include="..\..\src\Resend\Resend.csproj" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Resend.Webhooks;
2+
3+
namespace WebWebhook;
4+
5+
public class WebhookHandler : IWebhookHandler
6+
{
7+
private readonly ILogger<WebhookHandler> _logger;
8+
9+
10+
public WebhookHandler( ILogger<WebhookHandler> logger )
11+
{
12+
_logger = logger;
13+
}
14+
15+
16+
/// <summary />
17+
public async Task<IResult> HandleInvalid( WebhookContext context )
18+
{
19+
await Task.Delay( 0 );
20+
21+
return Results.NoContent();
22+
}
23+
24+
25+
/// <summary />
26+
public async Task<IResult> HandleValid( WebhookContext context )
27+
{
28+
await Task.Yield();
29+
30+
_logger.LogInformation( "", context.MessageId, context.Event );
31+
32+
return Results.NoContent();
33+
}
34+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

0 commit comments

Comments
 (0)