A modern, object-oriented .NET wrapper for the official Pastebin API. This library handles the heavy lifting of HTTP requests, XML parsing, and error management, providing a clean and intuitive interface for your applications.
- Modern, Type-Safe Design: Utilizes enums for options like
PasteExpiration,PastePrivate, andPasteFormat, preventing common errors and eliminating magic strings. - Clean C# Object Models: Deserializes XML API responses into simple, usable objects like
UserandPaste. - Full API Coverage: Implements all key features:
- Create pastes (public, private, unlisted).
- Log in to obtain a user session key.
- List a user's pastes.
- Retrieve user details.
- Delete user-owned pastes.
- Fetch the raw content of any paste.
- Designed for Testability: Allows injecting a custom
HttpClientfor straightforward unit and integration testing. - Robust Error Handling: Throws distinct exceptions for network failures and API-specific errors.
Since this library is provided as source code, integrate it directly into your solution:
- Clone this repository or download the source.
- Add the
PastebinApi/PastebinApi.csprojproject to your Visual Studio solution. - Add a project reference from your main application to the
PastebinApiproject.
To use the API, you need a Developer Key, which you can get from the API documentation page after logging in.
Instantiate the Pastebin client with your developer key.
using PastebinApi.Core;
// You must provide your developer key.
var api = new Pastebin("YOUR_DEVELOPER_KEY");Most actions require a user-specific key. You can obtain one by logging in. It's recommended to store and reuse this key.
using PastebinApi.Core;
using PastebinApi.Models.Enums;
// 1. Instantiate with your developer key
var api = new Pastebin("YOUR_DEVELOPER_KEY");
try
{
// 2. Log in to get a user key
string userKey = await api.LoginAsync("your_username", "your_password");
api.ApiUserKey = userKey; // Set the user key for subsequent requests
// 3. Create a new paste
string pasteUrl = await api.CreatePasteAsync(
pasteCode: "Hello, World from my .NET App!",
pasteName: "My First API Paste",
pasteExpiration: PasteExpiration.TenMinutes,
pastePrivate: PastePrivate.Unlisted,
pasteFormat: PasteFormat.Csharp
);
Console.WriteLine($"Paste created successfully! URL: {pasteUrl}");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}Once you have authenticated and set the ApiUserKey, you can perform other actions.
Retrieve a list of all pastes belonging to the authenticated user.
// Assumes 'api' is an authenticated client
var pastes = await api.GetAllPastesAsync(maxResults: 50);
Console.WriteLine($"Found {pastes.Length} pastes:");
foreach (var paste in pastes)
{
Console.WriteLine($"- {paste.PasteTitle} ({paste.PasteUrl}) created at {paste.CreatedAt}");
}Fetch details for the authenticated user.
var userInfo = await api.GetUserInfoAsync();
Console.WriteLine($"Username: {userInfo.UserName}");
Console.WriteLine($"Email: {userInfo.Email}");
Console.WriteLine($"Website: {userInfo.Website}");
Console.WriteLine($"Account Type: {userInfo.AccountType}"); // Normal or ProDelete a paste using its key (the part of the URL after pastebin.com/).
string pasteKeyToDelete = "0b42rwhf"; // Example key
string result = await api.DeletePasteAsync(pasteKeyToDelete);
Console.WriteLine(result); // "Paste Removed" on successRetrieve the raw text content of a paste.
string pasteKeyToRead = "0C343n0d"; // Example key
string rawContent = await api.GetRawPasteAsync(pasteKeyToRead);
Console.WriteLine("--- Paste Content ---");
Console.WriteLine(rawContent);The library will throw exceptions for failed operations:
HttpRequestException: For network-related issues (e.g., no internet connection, DNS failure).InvalidOperationException: For API-level errors returned by Pastebin (e.g., "Bad API request, invalid api_dev_key").
Always wrap your API calls in a try-catch block to handle potential failures gracefully.
This project is licensed under the MIT License. See the LICENSE file for details.