Skip to content

Repository files navigation

SerpApi .NET Library

NuGet Build

Integrate search data into your .NET application, AI workflow, or LLM/RAG pipeline. This is the official .NET client for SerpApi.

SerpApi supports Google, Google Maps, Google Shopping, Bing, Baidu, Yandex, Yahoo, DuckDuckGo, eBay, Walmart, YouTube, and 100+ engines.

Features

  • Async-first with full CancellationToken support
  • Sync convenience wrappers
  • IAsyncEnumerable pagination
  • Dependency injection integration (IHttpClientFactory)
  • Targets .NET Standard 2.0, .NET 7, 8, 9, and 10
  • Zero external runtime dependencies

Installation

dotnet add package serpapi

Simple Usage

using SerpApi;

using var client = new SerpApiClient(Environment.GetEnvironmentVariable("SERPAPI_KEY")!);

using var results = await client.SearchAsync(new Dictionary<string, string>
{
    ["engine"] = "google_light",
    ["q"] = "coffee"
});

foreach (var result in results.OrganicResults!.Value.EnumerateArray())
{
    Console.WriteLine(result.GetProperty("title").GetString());
}

Error handling

try
{
    using var results = await client.SearchAsync(parameters);
}
catch (SerpApiKeyException)       { /* 401 — invalid API key */ }
catch (SerpApiHttpException ex)   { /* 429, 500, etc — ex.StatusCode */ }
catch (SerpApiTimeoutException)   { /* request timed out */ }
catch (SerpApiException ex)       { /* catch-all */ }

Search API usage

Get JSON results

using var results = await client.SearchAsync(new Dictionary<string, string>
{
    ["engine"] = "google_light",
    ["q"] = "coffee",
    ["num"] = "10"
});

Console.WriteLine(results.SearchId);
Console.WriteLine(results.OrganicResults);
Console.WriteLine(results["local_results"]);

Get HTML results

string html = await client.HtmlAsync(new Dictionary<string, string>
{
    ["engine"] = "google_light",
    ["q"] = "coffee"
});

Pagination

// Next page
using var page2 = await client.NextPageAsync(results);

// Iterate all pages as an async stream
await foreach (var page in client.SearchPagesAsync(parameters, maxPages: 5))
{
    using (page)
    {
        Console.WriteLine($"Page has {page.OrganicResults?.GetArrayLength()} results");
    }
}

Search concurrently

var tasks = new[]
{
    client.SearchAsync(new Dictionary<string, string>
    {
        ["engine"] = "google_light", ["q"] = "coffee"
    }),
    client.SearchAsync(new Dictionary<string, string>
    {
        ["engine"] = "google_news_light", ["q"] = "coffee"
    })
};

try
{
    var results = await Task.WhenAll(tasks);
    // Process results here.
}
finally
{
    foreach (var task in tasks)
    {
        if (task.Status == TaskStatus.RanToCompletion)
            task.Result.Dispose();
    }
}

Location API

var locations = await client.LocationAsync("Austin, TX", limit: 3);
foreach (var loc in locations.EnumerateArray())
{
    Console.WriteLine(loc.GetProperty("name").GetString());
}

Search Archive API

Retrieve a previous search (0 credits):

using var archived = await client.SearchArchiveAsync("previous_search_id");

Account API

using var account = await client.AccountAsync();
Console.WriteLine(account["plan_id"]);

Basic examples per search engine

Search Google

using var results = await client.SearchAsync(new Dictionary<string, string>
{
    ["engine"] = "google",
    ["q"] = "coffee",
    ["location"] = "Austin, Texas"
});

Search Google Light

using var results = await client.SearchAsync(new Dictionary<string, string>
{
    ["engine"] = "google_light",
    ["q"] = "coffee"
});

Search Google Scholar

using var results = await client.SearchAsync(new Dictionary<string, string>
{
    ["engine"] = "google_scholar",
    ["q"] = "machine learning"
});

Search Google News

using var results = await client.SearchAsync(new Dictionary<string, string>
{
    ["engine"] = "google_news",
    ["q"] = "artificial intelligence"
});

Search Google Maps

using var results = await client.SearchAsync(new Dictionary<string, string>
{
    ["engine"] = "google_maps",
    ["q"] = "pizza",
    ["ll"] = "@40.7455096,-74.0083012,14z"
});

Search Google Shopping

using var results = await client.SearchAsync(new Dictionary<string, string>
{
    ["engine"] = "google_shopping",
    ["q"] = "laptop"
});

Search Google Jobs

using var results = await client.SearchAsync(new Dictionary<string, string>
{
    ["engine"] = "google_jobs",
    ["q"] = "software engineer"
});

Search Google Images

using var results = await client.SearchAsync(new Dictionary<string, string>
{
    ["engine"] = "google_images",
    ["q"] = "sunset"
});

Search Google Finance

using var results = await client.SearchAsync(new Dictionary<string, string>
{
    ["engine"] = "google_finance",
    ["q"] = "AAPL:NASDAQ"
});

Search Bing

using var results = await client.SearchAsync(new Dictionary<string, string>
{
    ["engine"] = "bing",
    ["q"] = "coffee"
});

Search DuckDuckGo

using var results = await client.SearchAsync(new Dictionary<string, string>
{
    ["engine"] = "duckduckgo",
    ["q"] = "coffee"
});

Search Baidu

using var results = await client.SearchAsync(new Dictionary<string, string>
{
    ["engine"] = "baidu",
    ["q"] = "coffee"
});

Search Yahoo

using var results = await client.SearchAsync(new Dictionary<string, string>
{
    ["engine"] = "yahoo",
    ["p"] = "coffee"
});

Search YouTube

using var results = await client.SearchAsync(new Dictionary<string, string>
{
    ["engine"] = "youtube",
    ["search_query"] = "latte art"
});

Search Walmart

using var results = await client.SearchAsync(new Dictionary<string, string>
{
    ["engine"] = "walmart",
    ["query"] = "coffee maker"
});

Search eBay

using var results = await client.SearchAsync(new Dictionary<string, string>
{
    ["engine"] = "ebay",
    ["_nkw"] = "laptop"
});

Search Amazon

using var results = await client.SearchAsync(new Dictionary<string, string>
{
    ["engine"] = "amazon",
    ["k"] = "coffee"
});

Search Naver

using var results = await client.SearchAsync(new Dictionary<string, string>
{
    ["engine"] = "naver",
    ["query"] = "coffee"
});

Search Apple App Store

using var results = await client.SearchAsync(new Dictionary<string, string>
{
    ["engine"] = "apple_app_store",
    ["term"] = "coffee"
});

Search Home Depot

using var results = await client.SearchAsync(new Dictionary<string, string>
{
    ["engine"] = "home_depot",
    ["q"] = "drill"
});

Configuration

using var client = new SerpApiClient("YOUR_API_KEY", new SerpApiClientOptions
{
    Timeout = TimeSpan.FromSeconds(30)
});

Dependency Injection

builder.Services.AddSerpApi(options =>
{
    options.ApiKey = builder.Configuration["SerpApi:ApiKey"]!;
    options.Timeout = TimeSpan.FromSeconds(30);
});

Uses IHttpClientFactory for connection management.

Resilience

Install the Microsoft.Extensions.Http.Resilience package:

dotnet add package Microsoft.Extensions.Http.Resilience
builder.Services.AddSerpApi(options =>
{
    options.ApiKey = builder.Configuration["SerpApi:ApiKey"]!;
})
.AddStandardResilienceHandler();

Corporate proxy

var handler = new HttpClientHandler
{
    Proxy = new WebProxy("http://proxy.corp.example:8080"),
    UseProxy = true
};
using var client = new SerpApiClient(
    new HttpClient(handler),
    new SerpApiClientOptions { ApiKey = "YOUR_API_KEY" });

Examples

See examples/ for runnable projects:

Example Use Case Description
LeadFinder Lead generation Find local businesses via Google Maps for sales outreach
CompetitorTracker SEO & competitive intel Monitor brand vs competitor SERP positions across engines
RankTracker SEO rank monitoring Track keyword positions page-by-page with pagination
PriceMonitor Price monitoring Compare product prices across Google Shopping and Walmart
AiResearchAgent AI/RAG pipelines Gather multi-source context (web + news + scholar) for LLMs
ContentDiscovery Market research Find trending topics, PAA questions, and content gaps
ErrorHandling Reliability Exception types, retry patterns, and graceful degradation
DependencyInjection Enterprise integration ASP.NET Core / generic host with IHttpClientFactory
export SERPAPI_KEY=your_key_here
cd examples/LeadFinder
dotnet run

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/serpapi/serpapi-dotnet.

git clone https://github.com/serpapi/serpapi-dotnet.git
cd serpapi-dotnet
dotnet build
dotnet test

License

MIT — see LICENSE.

About

SerpApi Client library for dotnet 5 and 6

Resources

Stars

4 stars

Watchers

6 watching

Forks

Releases

Packages

Used by

Contributors

Languages