← Integrations

Backend · C#

Chaldean numerology from an ASP.NET Core minimal API

ASP.NET Core already has the pieces: user-secrets for the key in development, IHttpClientFactory for a pooled typed client, and a minimal API endpoint. Wired to the Chaldean numerology endpoint, the one setting that matters is the JSON naming policy - get it wrong and half the response quietly binds as defaults.

Updated

How this page was checked: The C# was not executed - there is no .NET SDK on the build machine. The requests it sends (same path, headers and JSON body) were sent with Node 24 fetch to the in-memory API; every number and error message below is from those runs. The System.Text.Json behaviour described is the framework's documented default, not something observed here.

The key: user-secrets locally, AstroNode__ApiKey in production

bash
dotnet user-secrets init
dotnet user-secrets set "AstroNode:ApiKey" "aie_live_..."

User-secrets live in your user profile, outside the project folder, and are only loaded in the Development environment. In production, set an environment variable; configuration keys use : but environment variable names cannot on every OS, so AstroNode:ApiKey becomes AstroNode__ApiKey (double underscore). Never put the key in appsettings.json - that file is committed and copied into the publish output.

A typed client and the endpoint

AstroNodeClient.cs:

csharp
using System.Net.Http.Json;
using System.Text.Json;

public sealed class AstroNodeClient(HttpClient http)
{
    // Web defaults (camelCase, case-insensitive) + the API's snake_case names.
    static readonly JsonSerializerOptions Json = new(JsonSerializerDefaults.Web)
    {
        PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
    };

    public async Task<ChaldeanResult> ChaldeanAsync(string number, string name, DateOnly dob, CancellationToken ct = default)
    {
        var body = new ChaldeanRequest(name, dob.ToString("yyyy-MM-dd"), number);
        using var res = await http.PostAsJsonAsync("/v1/numerology/chaldean", body, Json, ct);
        var envelope = await res.Content.ReadFromJsonAsync<Envelope<ChaldeanData>>(Json, ct)
            ?? throw new AstroNodeException((int)res.StatusCode, "EMPTY_BODY", "no JSON in response");
        if (envelope.Error is { } e) throw new AstroNodeException((int)res.StatusCode, e.Code, e.Message);
        return envelope.Data!.Result;
    }
}

public record ChaldeanRequest(string Name, string Dob, string Number);
public record Envelope<T>(T? Data, ApiError? Error);
public record ApiError(string Code, string Message);
public record ChaldeanData(string Number, ChaldeanResult Result);
public record ChaldeanResult(int Value, bool IsMaster, int[] ReductionChain);

public sealed class AstroNodeException(int status, string code, string message)
    : Exception($"{status} {code}: {message}")
{
    public int Status { get; } = status;
    public string Code { get; } = code;
}

Program.cs:

csharp
using System.Net.Http.Headers;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddHttpClient<AstroNodeClient>(client =>
{
    var key = builder.Configuration["AstroNode:ApiKey"]
        ?? throw new InvalidOperationException("AstroNode:ApiKey is not configured");
    client.BaseAddress = new Uri(builder.Configuration["AstroNode:BaseUrl"] ?? "https://api.astronode.dev");
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", key);
    client.Timeout = TimeSpan.FromSeconds(15);
});

var app = builder.Build();

// GET /numerology/life-path?name=Ananya%20Sharma&dob=1994-03-21
app.MapGet("/numerology/{number}", async (string number, string name, DateOnly dob, AstroNodeClient astro) =>
{
    try
    {
        return Results.Ok(await astro.ChaldeanAsync(number, name, dob));
    }
    catch (AstroNodeException e) when (e.Status == 400)
    {
        return Results.BadRequest(new { e.Code, e.Message });
    }
});

app.Run();

AddHttpClient<AstroNodeClient> registers the client as transient and hands it an HttpClient from the factory's pooled handlers. That is what avoids the two classic failures: a new HttpClient() per request exhausting sockets, and one static client that never notices a DNS change.

What came back

"Ananya Sharma", born 21 March 1994
numbervaluereduction_chain
whole-name330 → 3
life-path229 → 11 → 2
birth-number321 → 3

The life path passes through 11 and still reduces to 2 with is_master: false: the Chaldean system reduces everything to a single digit. If your product promises master numbers, that is the Pythagorean endpoint's behaviour, not this one (Chaldean vs Pythagorean). Asking for whole-name without a name returned 400 VALIDATION_ERROR with a message saying that whole-name requires a name - the endpoint above passes it through as a 400 of its own.

The mistake that bites first: snake_case binding as defaults

With only JsonSerializerDefaults.Web, System.Text.Json looks for isMaster and reductionChain. The API sends is_master and reduction_chain; case-insensitive matching does not bridge an underscore, and unmatched JSON properties are skipped by default. So Value (one word) binds, IsMaster stays false and ReductionChain stays null - no exception, just wrong data.

  • Set PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower (.NET 8 and later), as above, or put [JsonPropertyName("is_master")] on each property for older versions.
  • The request side hides the problem here because name, dob and number are single words. This endpoint also does not reject unknown keys - a body with targetDate returned 200 - so a misnamed optional field is dropped without a 400. Birth-data endpoints are strict and would reject it.
  • Your own endpoint's JSON is unaffected: minimal APIs serialise ChaldeanResult to the browser with their own camelCase settings, which is usually what a front end expects.

POST /v1/numerology/chaldean needs the Hobby plan or above.

Questions

Which AstroNode endpoints does the ASP.NET Core example call?
1 endpoint: Chaldean Numerology (POST /v1/numerology/chaldean).
Was the ASP.NET Core code on this page actually run?
The C# was not executed - there is no .NET SDK on the build machine. The requests it sends (same path, headers and JSON body) were sent with Node 24 fetch to the in-memory API; every number and error message below is from those runs. The System.Text.Json behaviour described is the framework's documented default, not something observed here.