Authorization

Since version 4.0 the Authorization method sets the Authorization header for you, supporting the Bearer and Basic schemes. It works with the dynamic and static syntax, typed clients and the McpHandler alike.

Bearer scheme

var client = new RestClient("https://api.github.com");

var repositories = await client
   .Authorization(AuthenticationSchemes.Bearer, "your-token")
   .Resource("users/dalsoft/repos")
   .Get();

Basic scheme

Pass the username and password and RestClient will Base64 encode the header for you:

var client = new RestClient("https://api.example.com");

var result = await client
   .Authorization(AuthenticationSchemes.Basic, "username", "password")
   .Resource("users")
   .Get();

AuthenticationSchemes is in the DalSoft.RestClient namespace.

Other schemes

For any other scheme, or a token that applies to every request, set the Authorization header yourself - as a default header passed to the constructor or AddRestClient(), or per request with Headers():

var client = new RestClient("https://api.example.com", new Headers(new { Authorization = "ApiKey your-key" }));

For anything more involved - token refresh, OAuth flows - write a handler and it applies to every request through the client.

Updated: