GuardianClient.NET 0.5.0-alpha

This is a prerelease version of GuardianClient.NET.
dotnet add package GuardianClient.NET --version 0.5.0-alpha
                    
NuGet\Install-Package GuardianClient.NET -Version 0.5.0-alpha
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GuardianClient.NET" Version="0.5.0-alpha" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="GuardianClient.NET" Version="0.5.0-alpha" />
                    
Directory.Packages.props
<PackageReference Include="GuardianClient.NET" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add GuardianClient.NET --version 0.5.0-alpha
                    
#r "nuget: GuardianClient.NET, 0.5.0-alpha"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package GuardianClient.NET@0.5.0-alpha
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=GuardianClient.NET&version=0.5.0-alpha&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=GuardianClient.NET&version=0.5.0-alpha&prerelease
                    
Install as a Cake Tool

GuardianClient.NET


<p align="center"> <img src="./assets/guardian.png" width="150" alt="Guardian knight logo"/> </p>

A modern, comprehensive .NET client library for The Guardian's Content API. Provides strongly-typed models, flexible search options, and simple methods for searching Guardian articles with full API feature support.

NuGet Version Build Status

โœจ Features

  • ๐Ÿ” Complete Content Search - Full Guardian Content API search with all parameters
  • ๐Ÿท๏ธ Strongly Typed - Type-safe enums for fields, tags, elements, and references
  • ๐ŸŽฏ Comprehensive Filtering - Search by section, tags, date ranges, production office, and more
  • ๐Ÿ“‘ Rich Content Options - Include fields, tags, media elements, blocks, and references
  • ๐Ÿ”ง Interface-Based Design - Easy mocking and dependency injection with IGuardianApiClient
  • ๐Ÿ“ฆ HttpClientFactory Ready - Proper HttpClient lifecycle management
  • โšก Modern Async - Full async/await patterns with cancellation support
  • ๐ŸŽจ Clean API - Organized option classes for maintainable code

๐Ÿš€ Quick Start

Installation

dotnet add package GuardianClient.NET
// Program.cs or Startup.cs
builder.Services.AddGuardianApiClient("your-api-key");

Basic Usage

public class NewsService
{
    private readonly IGuardianApiClient _client;

    public NewsService(IGuardianApiClient client)
    {
        _client = client;
    }

    public async Task<ContentSearchResponse?> GetLatestTechNews()
    {
        return await _client.SearchAsync(new GuardianApiContentSearchOptions
        {
            Query = "artificial intelligence",
            FilterOptions = new GuardianApiContentFilterOptions
            {
                Section = "technology"
            },
            PageOptions = new GuardianApiContentPageOptions 
            { 
                PageSize = 10 
            }
        });
    }
}

๐Ÿ”ง Advanced Usage

Comprehensive Search with All Options

var results = await client.SearchAsync(new GuardianApiContentSearchOptions
{
    // Search terms with boolean operators
    Query = "climate change AND (policy OR legislation)",
    QueryFields = ["body", "headline"],
    
    // Filtering options
    FilterOptions = new GuardianApiContentFilterOptions
    {
        Section = "environment",
        Tag = "climate-change",
        Language = "en"
    },
    
    // Date filtering  
    DateOptions = new GuardianApiContentDateOptions
    {
        FromDate = new DateOnly(2023, 1, 1),
        UseDate = "published"
    },
    
    // Pagination
    PageOptions = new GuardianApiContentPageOptions
    {
        Page = 1,
        PageSize = 20
    },
    
    // Ordering
    OrderOptions = new GuardianApiContentOrderOptions
    {
        OrderBy = GuardianApiContentOrderBy.Relevance,
        OrderDate = GuardianApiContentOrderDate.Published
    },
    
    // Additional content
    AdditionalInformationOptions = new GuardianApiContentAdditionalInformationOptions
    {
        ShowFields = [
            GuardianApiContentShowFieldsOption.Headline,
            GuardianApiContentShowFieldsOption.Body,
            GuardianApiContentShowFieldsOption.Thumbnail
        ],
        ShowTags = [
            GuardianApiContentShowTagsOption.Keyword,
            GuardianApiContentShowTagsOption.Tone
        ],
        ShowElements = [GuardianApiContentShowElementsOption.Image]
    }
});

Getting Individual Articles

// Get a specific article by ID
var article = await client.GetItemAsync("world/2023/oct/15/climate-summit-agreement",
    new GuardianApiContentAdditionalInformationOptions
    {
        ShowFields = [
            GuardianApiContentShowFieldsOption.Body,
            GuardianApiContentShowFieldsOption.Byline
        ],
        ShowTags = [GuardianApiContentShowTagsOption.All]
    });

// Access the rich content
Console.WriteLine(article.Content.WebTitle);
Console.WriteLine(article.Content.Fields.Body); // Full HTML content
Console.WriteLine($"Author: {article.Content.Fields.Byline}");

Manual Setup (without DI)

using var client = new GuardianApiClient("your-api-key");
var results = await client.SearchAsync(new GuardianApiContentSearchOptions 
{
    Query = "sports",
    PageOptions = new GuardianApiContentPageOptions { PageSize = 5 }
});

foreach (var article in results.Results)
{
    Console.WriteLine($"{article.WebTitle} - {article.WebPublicationDate}");
}

๐Ÿ”‘ Getting an API Key

  1. Visit The Guardian Open Platform
  2. Sign up for a free developer account
  3. Generate your API key
  4. Store it securely (use User Secrets for development)

๐Ÿ—๏ธ Available Options

Filter Options

  • Section: Filter by Guardian sections (e.g., "politics", "sport", "culture")
  • Tags: Filter by content tags with boolean operators
  • Date Range: Filter by publication date with flexible date types
  • Language: Filter by content language (ISO codes)
  • Production Office: Filter by Guardian production offices
  • Star Rating: Filter by review ratings (1-5)

Additional Content Options

  • ShowFields: Include extra fields like body content, thumbnails, bylines
  • ShowTags: Include metadata tags (keywords, contributors, tone)
  • ShowElements: Include media elements (images, videos, audio)
  • ShowReferences: Include reference data (ISBNs, IMDB IDs, etc.)
  • ShowBlocks: Include content blocks (useful for live blogs)

Ordering Options

  • OrderBy: Sort by newest, oldest, or relevance
  • OrderDate: Choose which date to use for sorting

๐Ÿ“Š Current Status

โœ… Fully Implemented:

  • Complete Content API search with all parameters
  • Individual article retrieval
  • Strongly-typed models and enums for all options
  • Advanced filtering, pagination, and sorting
  • Rich content enhancement options
  • Interface-based design for easy testing
  • Comprehensive documentation and examples

๐ŸŽฏ Feature Complete: This library now supports the full Guardian Content API specification.

๐Ÿงช Testing

The library includes comprehensive test coverage:

# Run all tests
dotnet test

# Run with detailed output
dotnet test --logger "console;verbosity=detailed"

Tests require a Guardian API key stored in user secrets:

dotnet user-secrets set "GuardianApiKey" "your-api-key-here"

๐Ÿค Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Ensure all tests pass
  5. Submit a pull request

๐Ÿ“„ License

MIT License - see LICENSE for details.


This is an unofficial library and is not affiliated with The Guardian.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.5.0-alpha 124 8/13/2025
0.4.0-alpha 121 8/12/2025
0.3.0-alpha 118 8/11/2025
0.2.1-alpha 115 8/10/2025
0.2.0-alpha 114 8/10/2025
0.1.0-alpha 118 8/10/2025