API tests in C# with HttpClient: no extra framework needed
By Denis Bojkovski, Test Automation Architect · Published November 25, 2026
Why test at this level
An API test exercises business rules without a browser: faster, steadier and closer to the fault. APIs were mentioned in 37 of the 86 full descriptions on QAAnywhere on 25 November 2026. Postman is excellent for exploring; once checks must live beside the code, be reviewed and run on every change, plain C# is a good home.
A small typed client
using System.Net;
using System.Net.Http.Json;
public sealed record Order(string Id, string Status, decimal Total);
public sealed class OrdersClient(HttpClient http)
{
public Task<HttpResponseMessage> CreateAsync(object body, string? key = null)
{
var request = new HttpRequestMessage(HttpMethod.Post, "orders")
{
Content = JsonContent.Create(body),
};
if (key is not null) request.Headers.Add("Idempotency-Key", key);
return http.SendAsync(request);
}
public Task<HttpResponseMessage> GetAsync(string id) => http.GetAsync(quot;orders/{id}");
}
Return the response rather than a parsed object. Tests of error cases need the status and headers.
A test that says what matters
[Test]
public async Task Creating_an_order_returns_its_location_and_total()
{
var response = await orders.CreateAsync(new { sku = "MUG-1", quantity = 2 });
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Created));
Assert.That(response.Headers.Location, Is.Not.Null);
var order = await response.Content.ReadFromJsonAsync<Order>();
Assert.Multiple(() =>
{
Assert.That(order!.Status, Is.EqualTo("new"));
Assert.That(order.Total, Is.EqualTo(24.00m));
});
}
Use decimals for money. Assert on the fields the rule concerns, not the whole document: comparing everything breaks each time somebody adds a field.
Cover more than the happy path
For each endpoint, go through this list:
- Validation: missing, empty, too long, wrong type, zero, negative, boundary values.
- Authentication: no token, expired token.
- Authorisation: a valid user asking for somebody else's order. This is the check most often missing, and the most serious when it fails.
- Not found against not allowed: does the response reveal that the record exists?
- Content type and caching headers.
- Pagination: first, last and beyond-last page; a stable order.
- Error body: same shape everywhere, with no stack trace.
Test idempotency
Networks fail after the server has done its work, so clients retry. A create endpoint that accepts an idempotency key should produce one order for two identical calls:
[Test]
public async Task Same_key_creates_one_order()
{
var key = Guid.NewGuid().ToString();
var body = new { sku = "MUG-1", quantity = 1 };
var first = await (await orders.CreateAsync(body, key)).Content.ReadFromJsonAsync<Order>();
var second = await (await orders.CreateAsync(body, key)).Content.ReadFromJsonAsync<Order>();
Assert.That(second!.Id, Is.EqualTo(first!.Id));
}
Then send the two calls at the same time with Task.WhenAll. Many implementations pass the sequential test and fail the concurrent one.
Practicalities
- Share one
HttpClientfor a run. Creating one per request can exhaust sockets. - Set a timeout. The default is 100 seconds, too long for a test.
- Put request and response in the failure output, with tokens masked.
- Fetch a token once per role, and refresh it before it expires.
- For a .NET service you own,
WebApplicationFactoryhosts it in memory, so tests run without a deployed environment. Keep a handful against the real one.
Where this ends
These tests check one service's behaviour. Whether two services still agree is the subject of contract testing. For the tools employers name most, see API testing for remote QA jobs.