Some text-processing APIs answer in the same request. Others accept a job, give you a task ID and expect you to come back for the result. The second kind is easy to get wrong in C#: you block a thread, poll too fast, or forget the statuses that aren't "done".
This .NET 8 example calls the AI humanizer API from AI Checker Humanizer using HttpClient and System.Net.Http.Json. For another submit-then-poll API, update the statuses that stop WaitAsync and the HTTP responses that tell the caller when to retry.
The contract
The rewrite endpoint works in two steps:
POST /api/v1/humanizewith the text and an optionalmodereturns202 Acceptedand ataskId.GET /api/v1/humanize/{taskId}returns the task. Whilestatusisqueuedorrunning, wait and ask again. The documentation suggests polling about every 3 seconds.
The API has five final statuses: completed, partial, unchanged, failed and cancelled. Only completed and partial include rewritten text in output, and both return the full document: with partial, chunks that weren't rewritten stay as you submitted them. Fetch the result before expiresAt, about 70 minutes after you submit.
Send the API key as an Authorization: Bearer header. The example reads the key from the AICH_API_KEY environment variable.
The client
using System.Net;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json.Serialization;
public record HumanizeRequest(
[property: JsonPropertyName("text")] string Text,
[property: JsonPropertyName("mode")] string Mode = "standard");
public record HumanizeTask(
[property: JsonPropertyName("taskId")] string TaskId,
[property: JsonPropertyName("status")] string Status,
[property: JsonPropertyName("output")] string? Output,
[property: JsonPropertyName("reason")] string? Reason,
[property: JsonPropertyName("creditsCharged")] int? CreditsCharged,
[property: JsonPropertyName("expiresAt")] DateTimeOffset? ExpiresAt);
public sealed class HumanizeClient
{
private readonly HttpClient _http;
public HumanizeClient(HttpClient http, string apiKey)
{
_http = http;
_http.BaseAddress = new Uri("https://aicheckerhumanizer.com/api/v1/");
_http.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", apiKey);
}
public async Task<HumanizeTask> SubmitAsync(
string text, string idempotencyKey, CancellationToken ct)
{
using var request = new HttpRequestMessage(HttpMethod.Post, "humanize")
{
Content = JsonContent.Create(new HumanizeRequest(text))
};
request.Headers.Add("Idempotency-Key", idempotencyKey);
return await SendAsync(request, ct);
}
public async Task<HumanizeTask> WaitAsync(string taskId, CancellationToken ct)
{
while (true)
{
using var request = new HttpRequestMessage(HttpMethod.Get, $"humanize/{taskId}");
var task = await SendAsync(request, ct);
if (task.Status is not ("queued" or "running"))
return task;
await Task.Delay(TimeSpan.FromSeconds(3), ct);
}
}
private async Task<HumanizeTask> SendAsync(HttpRequestMessage request, CancellationToken ct)
{
using var response = await _http.SendAsync(request, ct);
if (response.StatusCode == HttpStatusCode.TooManyRequests)
{
var wait = response.Headers.RetryAfter?.Delta ?? TimeSpan.FromSeconds(30);
throw new RateLimitedException(wait);
}
response.EnsureSuccessStatusCode();
return (await response.Content.ReadFromJsonAsync<HumanizeTask>(cancellationToken: ct))!;
}
}
public sealed class RateLimitedException(TimeSpan retryAfter)
: Exception($"Rate limited; retry after {retryAfter}.")
{
public TimeSpan RetryAfter { get; } = retryAfter;
}
Join the conversation! Your thoughts help the community grow.