When consuming modern APIs, Bearer Token Authentication is a common method of securing endpoints. Instead of sending a username and password with every request, you send a token—usually a JWT (JSON Web Token) or other token string—that was previously issued by an authentication server.
This blog provides code examples, you'll learn how to perform authenticated API requests using Bearer tokens with RestSharp, a powerful and user-friendly HTTP client library for .NET.
Bearer Token Authentication With RestSharp
Getting Started
Whether you're building internal tools, automation scripts, or production applications, RestSharp simplifies the process of integrating with APIs.
The below two examples describes how to send request to API get and post method using ReestClient(RestSharp). The request format is JSON format and in the header bearer token is included for authentication.
Bearer Token Example
string token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6ImppYk5ia0ZTU2JteFBZck45Q0ZxUms0SzRndyIsImtpZCI6ImppYk5ia0ZTU2JteFBZck45Q0ZxUms0SzRndyJ9.eyJhdWQiOiJhcGk6Ly9jMTFjMzQzNi0wMjk4LTQ3NTMtYjAzNC1lZDhjMDg3YjRkZTgiLCJpc3MiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC8xMzFmZDcxZi0xOGVmLTQ0ODMtYjA0Mi0yZWQ1NDBiMjBhODUvIiwiaWF0IjoxNjAwODQwMjUyLCJuYmYiOjE2MDA4NDAyNTIsImV4cCI6MTYwMDg0NDE1MiwiYWlvIjoiRTJCZ1lGajN2bkZSMDQ0MjN4bE11cjhuU3E5K0FnQT0iLCJhcHBpZCI6ImMxMWMzNDM2LTAyOTgtNDc1My1iMDM0LWVkOGMwODdiNGRlOCIsImFwcGlkYWNyIjoiMSIsImlkcCI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0LzEzMWZkNzFmLTE4ZWYtNDQ4My1iMDQyLTJlZDU0MGIyMGE4NS8iLCJvaWQiOiJlMGVjMTJmNy1jYjk4LTQxZGMtOTU2NC1kNjdiNjAyMDY0MTkiLCJyaCI6IjAuQUFBQUg5Y2ZFLThZZzBTd1FpN1ZRTElLaFRZMEhNR1lBbE5Ic0RUdGpBaDdUZWdLQUFBLiIsInN1YiI6ImUwZWMxMmY3LWNiOTgtNDFkYy05NTY0LWQ2N2I2MDIwNjQxOSIsInRpZCI6IjEzMWZkNzFmLTE4ZWYtNDQ4My1iMDQyLTJlZDU0MGIyMGE4NSIsInV0aSI6IkNqbl95N2VQWFVxUUR1REFQeExBQVEiLCJ2ZXIiOiIxLjAifQ.emGSXtK4lzERav1v2_K6nZlmAevsGzHhObUMlJdhOmQVZqcX76sWlLcRxmvh-Sw0JBj9ya0n1fTGFklijfLyMxUVIKAIgs5pKUpAkDzz0E6qbQWKTf89yy7w7qKAdQnLySzxKdg672dsAOCeDga2FvbHAKWtyKpA9YdmkQJE0rqF8uUwrBRxTpjOUkQkz7XqFCBde2FxmDGHAp_75Yh9za8Ma0XSwi90gZdjNfasX2rxelScyLWjrb8kTmjZ_ZxYgKOqCSUqhw8swvYnlfpN8qknXXQGFnZBqQV3nXqF45vXIcF0HxoOo9gw-mo58FfoZiILH4KucVA2NDVXyRjURw";
Restsharp Bearer Token Example
Bearer Token Authentication with GET method
The following code example calls the API’s get method using RestClient and includes the bearer token in the request header
IRestRequest request = new RestRequest(Method.GET);
request.AddHeader("content-type", "application/json");
request.AddHeader("content-Length","0");
request.AddHeader("Authorization", "Bearer " + token);
request.RequestFormat = DataFormat.Json;
request.AddParameter("Parameter_Name", "Parameter_Value", ParameterType.RequestBody);
RestClient client = new RestClient("url");
IRestResponse response = client.Execute(request);
string responsevalue = response.Content;
Bearer Token Authentication
Bearer Token Authentication with POST method
The following code example calls the API’s POST method using RestClient and includes the bearer token in the request header
IRestRequest request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/json");
request.AddHeader("content-Length","0");
request.AddHeader("Authorization", "Bearer " + token);
request.RequestFormat = DataFormat.Json;
request.AddParameter("Parameter_Name", "Parameter_Value", ParameterType.RequestBody);
RestClient client = new RestClient("url");
IRestResponse response = client.Execute(request);
string responsevalue = response.Content;
Bearer Token Authentication
Summary
Using Bearer Token Authentication with RestSharp in C# is straightforward and essential for working with most modern APIs. With just a few lines of code, you can securely authenticate your HTTP requests and interact with protected endpoints.
In this blog, we learn how to request API with bearer token authentication using restclient. I hope you have enjoyed it a lot.
Thanks