# RestSharp — Simple REST API Client for .NET > A mature .NET HTTP client library that simplifies REST API consumption with automatic serialization, authentication helpers, and a fluent request builder. ## Install Save in your project root: # RestSharp — Simple REST API Client for .NET ## Quick Use ```bash dotnet add package RestSharp ``` ```csharp var client = new RestClient("https://api.example.com"); var request = new RestRequest("users/{id}").AddUrlSegment("id", 42); var user = await client.GetAsync(request); ``` ## Introduction RestSharp is a lightweight HTTP client library for .NET that wraps HttpClient with a higher-level API. It handles JSON and XML serialization, authentication, parameter binding, and file uploads, reducing the boilerplate required when calling REST APIs. ## What RestSharp Does - Sends GET, POST, PUT, PATCH, DELETE requests with typed responses - Serializes and deserializes JSON and XML automatically - Supports OAuth1, OAuth2, JWT, and basic authentication - Handles file uploads and multipart form data - Provides URL segment, query parameter, and header binding ## Architecture Overview RestSharp v107+ is built on top of HttpClient and HttpRequestMessage, acting as a convenience wrapper rather than a standalone HTTP stack. The RestClient manages an HttpClient instance and applies serializers, authenticators, and interceptors to each request. Serialization is pluggable, with System.Text.Json as the default and Newtonsoft.Json available as an add-on. ## Self-Hosting & Configuration - Install via NuGet: `dotnet add package RestSharp` - For Newtonsoft.Json: `dotnet add package RestSharp.Serializers.NewtonsoftJson` - Create one RestClient per base URL and reuse it (it wraps HttpClient) - Configure default headers, authenticators, and base URL in the constructor - Compatible with .NET 6 through .NET 9 ## Key Features - Fluent request builder with strong typing for parameters - Built-in authenticators for OAuth1, OAuth2, and JWT bearer tokens - Interceptor pipeline for modifying requests and responses - Automatic content-type negotiation and deserialization - Download-to-stream support for large file responses ## Comparison with Similar Tools - **HttpClient (raw)** — built into .NET; lower-level, requires manual serialization - **Refit** — interface-based REST client with source generation; less flexibility, more type safety - **Flurl** — fluent URL builder and HTTP client; similar convenience, different API style - **WebRequest (legacy)** — deprecated .NET API; should not be used in new code - **RestEase** — interface-driven REST client; lighter than Refit, smaller community ## FAQ **Q: Should I create one RestClient per request?** A: No. RestClient wraps HttpClient internally and should be reused per base URL. Creating a new instance per request can lead to socket exhaustion. **Q: How do I handle API rate limiting?** A: Use RestSharp interceptors to inspect response headers for rate-limit data and implement backoff logic, or combine with Polly for retry policies. **Q: Can RestSharp upload files?** A: Yes. Use `request.AddFile("field", filePath)` to attach files as multipart form data. **Q: What changed in RestSharp v107?** A: RestSharp v107 was a major rewrite that moved from a custom HTTP stack to wrapping HttpClient. It dropped sync methods, simplified the API, and made the library lighter. ## Sources - https://github.com/restsharp/RestSharp - https://restsharp.dev/ --- Source: https://tokrepo.com/en/workflows/asset-a0b1ba08 Author: AI Open Source