Skip to the content.

How to send a curl bearer token in C#

To send a curl bearer token in C#, include -H "Authorization: Bearer <token>" in the command you pass to Curl.ExecuteAsync, or call .WithBearerToken(token) on CurlRequestBuilder or LibCurl.

dotnet add package CurlDotNet

The curl command

using CurlDotNet;

var result = await Curl.ExecuteAsync(
    "curl -H 'Authorization: Bearer abc123' https://httpbin.org/bearer");

Console.WriteLine(result.StatusCode);  // 200 (401 without the header)

Keep the token out of your source code

The parser expands environment variables written as $NAME, ${NAME}, %NAME% or $env:NAME, so a command copied from API docs works as soon as the variable is set:

using CurlDotNet;

// API_TOKEN is set in the environment, for example by your CI system
var result = await Curl.ExecuteAsync(
    "curl -H \"Authorization: Bearer $API_TOKEN\" https://httpbin.org/bearer");

Console.WriteLine(result.StatusCode);

As in bash, the expansion happens in double quotes or unquoted text, but not inside single quotes: -H 'Authorization: Bearer $API_TOKEN' sends the text $API_TOKEN literally. If the variable is not set, the text is also sent as typed, and the API answers 401.

curl’s --oauth2-bearer <token> works too and adds the same header. Like -H 'Authorization: ...', the token is not forwarded to a different host on a redirect unless you add --location-trusted.

The fluent builder

using CurlDotNet.Core;

var token = Environment.GetEnvironmentVariable("API_TOKEN") ?? "";

var result = await CurlRequestBuilder
    .Get("https://httpbin.org/bearer")
    .WithBearerToken(token)
    .ExecuteAsync();

Console.WriteLine(result.StatusCode);

WithAuth("Token xyz") sets an arbitrary Authorization header value for APIs that use another scheme.

Reuse a token across many calls

CurlDotNet.Lib.LibCurl keeps default headers for every request it sends:

using CurlDotNet.Lib;

using var curl = new LibCurl();
curl.WithBearerToken(Environment.GetEnvironmentVariable("API_TOKEN") ?? "");

var me = await curl.GetAsync("https://httpbin.org/bearer");
var other = await curl.GetAsync("https://httpbin.org/headers");
Console.WriteLine($"{me.StatusCode} {other.StatusCode}");

Handle an expired or wrong token

A 401 or 403 does not throw (unless you add -f, which turns it into CurlHttpReturnedErrorException). Check the status and refresh the token:

using CurlDotNet;

var result = await Curl.ExecuteAsync("curl https://httpbin.org/bearer");
if (result.StatusCode == 401)
    Console.WriteLine("Token missing or expired - refresh it and retry.");

Security notes