Skip to content

larkliy/PastebinApi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pastebin API for .NET

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.

License: MIT

Features

  • Modern, Type-Safe Design: Utilizes enums for options like PasteExpiration, PastePrivate, and PasteFormat, preventing common errors and eliminating magic strings.
  • Clean C# Object Models: Deserializes XML API responses into simple, usable objects like User and Paste.
  • 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 HttpClient for straightforward unit and integration testing.
  • Robust Error Handling: Throws distinct exceptions for network failures and API-specific errors.

Setup

Since this library is provided as source code, integrate it directly into your solution:

  1. Clone this repository or download the source.
  2. Add the PastebinApi/PastebinApi.csproj project to your Visual Studio solution.
  3. Add a project reference from your main application to the PastebinApi project.

Getting Started

To use the API, you need a Developer Key, which you can get from the API documentation page after logging in.

1. Create an API Client

Instantiate the Pastebin client with your developer key.

using PastebinApi.Core;

// You must provide your developer key.
var api = new Pastebin("YOUR_DEVELOPER_KEY");

2. Log In and Create a Paste

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}");
}

API Usage Examples

Once you have authenticated and set the ApiUserKey, you can perform other actions.

Listing a User's Pastes

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}");
}

Getting User Information

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 Pro

Deleting a Paste

Delete 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 success

Getting Raw Paste Content

Retrieve 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);

Error Handling

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.

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

🎲Lightweight and simple Pastebin Api Wrapper to C#

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages