.NET
Install the HuntAPI .NET SDK and make your first API call.
The official .NET SDK for HuntAPI, with one typed method per endpoint and a generic request() for anything else.
Requirements
- .NET 8+
Install
dotnet add package HuntApiGet your API key
Copy your API key from the HuntAPI dashboard and set it in the HUNTAPI_API_KEY environment variable, which the SDK reads by default. Keep it out of source control.
Quick start
using HuntApi;
using var client = new HuntApiClient();
var response = await client.MediaMetadataAsync(new MediaMetadataParams { Query = "https://www.youtube.com/watch?v=Z5YP8kznWZs" });
Console.WriteLine(response.Json);Async jobs
Download endpoints are asynchronous: they return a job_id, and jobs can run for up to 12 hours. The SDK can wait for the job to finish.
var submitted = await client.VideoDownloadAsync(new VideoDownloadParams { Query = "https://www.youtube.com/watch?v=Z5YP8kznWZs", VideoQuality = "720p" });
var job = await client.WaitForJobAsync(
submitted.Json.GetProperty("job_id").GetString()!,
onProgress: j => Console.WriteLine($"{j.GetProperty("status")} {j.GetProperty("progress")}%"));Errors
A non-2xx response raises an API error with its status and message. See Status codes for the full list.
try
{
await client.MediaMetadataAsync(new MediaMetadataParams { Query = "https://www.youtube.com/watch?v=Z5YP8kznWZs" });
}
catch (HuntApiApiException exception)
{
Console.Error.WriteLine($"{exception.StatusCode}: {exception.Message}");
}Retries
Requests are retried up to 3 times after a network error, a 408, 500, 502, 503 or 504, or a plan rate limit. Per-key quota errors and client-side timeouts are not retried. The delay starts at 1 second and grows 1.5× per retry, and a Retry-After header wins.
using var client = new HuntApiClient(new HuntApiClientOptions { MaxRetries = 5, RetryDelay = TimeSpan.FromMilliseconds(500) });
await client.MediaMetadataAsync(new MediaMetadataParams { Query = "https://www.youtube.com/watch?v=Z5YP8kznWZs" }, new RequestOptions { MaxRetries = 0 }); // one request without retriesBatch requests
Run many calls at once, 10 at a time by default. One failure does not stop the others.
string[] inputs = { "https://www.youtube.com/watch?v=Z5YP8kznWZs", "https://www.youtube.com/watch?v=jNQXAC9IVRw" };
var results = await client.BatchAsync(inputs, (value, ct) =>
client.MediaMetadataAsync(new MediaMetadataParams { Query = value }, cancellationToken: ct), concurrency: 5);
foreach (var result in results)
{
if (result.Ok)
{
Console.WriteLine(result.Value!.Json);
}
else
{
Console.Error.WriteLine($"{inputs[result.Index]}: {result.Error!.Message}");
}
}Any endpoint
request() reaches any path, including endpoints released after your SDK version.
var response = await client.RequestAsync("GET", "/v1/usage");Common mistakes
- Putting the API key in front-end code. Anyone could read it. Call the API from your server.
- Treating every
401as a bad key. A401can also be a rate limit: read the message. The SDK retries plan rate limits for you. - Retrying a
402. It means there are no credits or quota left, so a retry cannot succeed. See Credits. - Waiting on long requests. Past 60 seconds, use async mode as shown above rather than raising your timeout. See Timeout.
Links
- Package:
HuntApi - Source code: github.com/huntapi/huntapi-dotnet
- API reference