Skip to content

Instantly share code, notes, and snippets.

@warrenkc
Created November 15, 2024 02:11
Show Gist options
  • Save warrenkc/562da58e06beea3c6735b181c5d37702 to your computer and use it in GitHub Desktop.
Save warrenkc/562da58e06beea3c6735b181c5d37702 to your computer and use it in GitHub Desktop.
// https://documentation.mailgun.com/docs/mailgun/api-reference/openapi-final/tag/Routes/#tag/Routes/operation/post-v3-routes
// This sample works with .net framework
private static async Task<Route> CreateRoute(string recipientAddress, string forwardAddress)
{
RestClientOptions options = new RestClientOptions(new Uri("https://api.mailgun.net/v3"));
options.Authenticator = new HttpBasicAuthenticator("api", ApiKey);
using (RestClient client = new RestClient(options))
{
//Url encoded format, example: https://example.com/api?priority=0&description=it%27s+a+new+route&expression=match_recipient%28%22user%40example.com%22%29&action=%5B%27forward%28%22forward%40example.com%22%29%27%5D
RestRequest request = new RestRequest();
request.Resource = "/routes";
request.AddParameter("priority", "0");
request.AddParameter("description", "it's a new route");
request.AddParameter("expression", $"match_recipient(\"{recipientAddress}\")");
request.AddParameter("action", $"forward(\"{forwardAddress}\")");
var response = await client.ExecutePostAsync(request);
// Parse the JSON and select only the "route" part
JObject jsonObject = JObject.Parse(response.Content);
Route route = jsonObject["route"].ToObject<Route>();
return route;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment