ApiToolkit.Net
1.0.4
dotnet add package ApiToolkit.Net --version 1.0.4
NuGet\Install-Package ApiToolkit.Net -Version 1.0.4
<PackageReference Include="ApiToolkit.Net" Version="1.0.4" />
paket add ApiToolkit.Net --version 1.0.4
#r "nuget: ApiToolkit.Net, 1.0.4"
// Install ApiToolkit.Net as a Cake Addin
#addin nuget:?package=ApiToolkit.Net&version=1.0.4
// Install ApiToolkit.Net as a Cake Tool
#tool nuget:?package=ApiToolkit.Net&version=1.0.4
<div align="center">
.NET SDK
APItoolkit is an end-to-end API and web services management toolkit for engineers and customer support teams. To integrate .Net
web services with APItoolkit, you need to use this SDK to monitor incoming traffic, aggregate the requests, and then deliver them to the APItoolkit's servers.
</div>
Table of Contents
- Installation
- Configuration
- Redacting Sensitive Data
- Monitoring outgiong requests
- Error Reporting
- Contributing and Help
- License
Installation
Kindly run the following command to install the package:
dotnet add package ApiToolkit.Net
Configuration
Now you can initialize APItoolkit in your application's entry point (e.g Program.cs
) like so:
var config = new Config
{
Debug = true, # Set debug flags to false in production
ApiKey = "{Your_APIKey}"
};
var client = await APIToolkit.NewClientAsync(config);
# Register the middleware to use the initialized client
app.Use(async (context, next) =>
{
var apiToolkit = new APIToolkit(next, client);
await apiToolkit.InvokeAsync(context);
});
# app.UseEndpoint(..)
# other middleware and logic
# ...
[!NOTE]
Please make sure the APItoolkit middleware is added before
UseEndpoint
and other middleware are initialized.
[!IMPORTANT]
The
{Your_APIKey}
field should be replaced with the API key generated from the APItoolkit dashboard.
Redacting Sensitive Data
If you have fields that are sensitive and should not be sent to APItoolkit servers, you can mark those fields to be redacted in two ways:
- This client SDK (the fields will never leave your servers in the first place).
- The APItoolkit dashboard (the fields will be transported from your servers first and then redacted on the edge before further processing).
To mark a field for redacting via this SDK, you need to provide additional arguments to the config
variable with paths to the fields that should be redacted. There are three (3) potential arguments that you can provide to configure what gets redacted.
RedactHeaders
: A list of HTTP header keys (e.g.,COOKIE
(redacted by default),CONTENT-TYPE
, etc.).RedactRequestBody
: A list of JSONPaths from the request body (if the request body is a valid JSON).RedactResponseBody
: A list of JSONPaths from the response body (if the response body is a valid JSON).
JSONPath Example
Given the following JSON object:
{
"store": {
"books": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
...
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
...
}
Examples of valid JSONPaths would be:
$.store.books
(In this case, APItoolkit will replace thebooks
field inside the store object with the string[CLIENT_REDACTED]
).$.store.books[*].author
(In this case, APItoolkit will replace theauthor
field in all the objects in thebooks
list inside thestore
object with the string[CLIENT_REDACTED]
).
For more examples and a detailed introduction to JSONPath, please take a look at this guide or this cheatsheet.
Configuration Example
Here's an example of what the configuration in your entry point (Program.cs
) would look like with the redacted fields configured:
var config = new Config
{
Debug = true, # Set debug flags to false in production
ApiKey = "{Your_APIKey}",
RedactHeaders = new List<string> { "HOST", "CONTENT-TYPE" },
RedactRequestBody = new List<string> { "$.password", "$.payment.credit_cards[*].cvv", "$.user.addresses[*]" },
RedactResponseBody = new List<string> { "$.title", "$.store.books[*].author" }
};
var client = await APIToolkit.NewClientAsync(config);
# Register the middleware to use the initialized client
app.Use(async (context, next) =>
{
var apiToolkit = new APIToolkit(next, client);
await apiToolkit.InvokeAsync(context);
})
[!NOTE]
While the
RedactHeaders
config field accepts a list of case-insensitive headers,RedactRequestBody
andRedactResponseBody
expect a list of JSONPath strings as arguments. Also, the list of items to be redacted will be applied to all endpoint requests and responses on your server.
Monitoring Outgoing Requests
Apitoolkit allows your to monitor request you make from your application just like request that come into your app. Outgoing request are associated with the request that triggers them when the request context is passed otherwise they appear as a standalone log in the APIToolkit log explorer. To monitor outgoing request
Example
using ApiToolkit.Net;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
var config = new Config
{
ApiKey = "<YOUR_API_KEY>"
};
var client = await APIToolkit.NewClientAsync(config);
app.Use(async (context, next) =>
{
var apiToolkit = new APIToolkit(next, client);
await apiToolkit.InvokeAsync(context);
});
app.MapGet("/monitor-requets", async (context) =>
{
using var httpClient = new HttpClient(client.APIToolkitObservingHandler(context));
var response = await httpClient.GetAsync("https://jsonplaceholder.typicode.com/posts/1");
var body = await response.Content.ReadAsStringAsync();
await context.Response.WriteAsync(body);
});
The observing handler also take and optional configuration options which include the following fields
PathWildCard
: For urls with path params setting PathWildCard will be used as the url_path
RedactHeaders
: A string list of headers to redact
RedactResponseBody
: A string list of json paths to redact from response body
RedactRequestBody
: A string list of json paths to redact from request body
Full configuration example
using ApiToolkit.Net;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
var config = new Config
{
ApiKey = "<YOUR_API_KEY>"
};
var client = await APIToolkit.NewClientAsync(config);
app.Use(async (context, next) =>
{
var apiToolkit = new APIToolkit(next, client);
await apiToolkit.InvokeAsync(context);
});
app.MapGet("/monitor-requets", async (context) =>
{
var observingHandlerOptions = new ATOptions
{
PathWildCard = "/posts/{id}", // url_path will be /posts/{id} instead of /posts/1
RedactHeaders = ["User-Agent"],
RedactRequestBody = ["$.user.password"],
RedactResponseBody = ["$.user.data.email"]
};
using var httpClient = new HttpClient(client.APIToolkitObservingHandler(context, observingHandlerOptions));
var response = await httpClient.GetAsync("https://jsonplaceholder.typicode.com/posts/1");
var body = await response.Content.ReadAsStringAsync();
await context.Response.WriteAsync(body);
});
Error Reporting
APIToolkit detects a lot of API issues automatically, but it's also valuable to report and track errors.
This helps you associate more details about the backend with a given failing request. If you've used sentry, or rollback, or bugsnag, then you're likely aware of this functionality.
To report errors, simply call ReportError
method of the APIToolkit client
Example
using ApiToolkit.Net;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
var config = new Config
{
ApiKey = "<YOUR_API_KEY>"
};
var client = await APIToolkit.NewClientAsync(config);
app.Use(async (context, next) =>
{
var apiToolkit = new APIToolkit(next, client);
await apiToolkit.InvokeAsync(context);
});
app.MapGet("/error-tracking", async context =>
{
try
{
// Attempt to open a non-existing file
using (var fileStream = System.IO.File.OpenRead("nonexistingfile.txt"))
{
// File opened successfully, do something if needed
}
await context.Response.WriteAsync($"Hello, {context.Request.RouteValues["name"]}!");
}
catch (Exception error)
{
// Report error to apitoolkit (associated with the request)
client.ReportError(context, error);
await context.Response.WriteAsync("Error reported!");
}
});
Contributing and Help
To contribute to the development of this SDK or request help from the community and our team, kindly do any of the following:
- Read our Contributors Guide.
- Join our community Discord Server.
- Create a new issue in this repository.
License
This repository is published under the MIT license.
<div align="center">
<a href="https://apitoolkit.io?utm_source=apitoolkit_github_dotnetsdk" target="_blank" rel="noopener noreferrer"><img src="https://github.com/apitoolkit/.github/blob/main/images/icon.png?raw=true" width="40" /></a>
</div>
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
-
net7.0
- Google.Apis.Auth (>= 1.62.0)
- Google.Cloud.PubSub.V1 (>= 3.7.0)
- Microsoft.AspNetCore.Hosting (>= 2.2.7)
- Microsoft.AspNetCore.Http (>= 2.2.2)
- Microsoft.AspNetCore.Http.Extensions (>= 2.2.0)
- Microsoft.AspNetCore.Routing (>= 2.2.2)
- Microsoft.AspNetCore.TestHost (>= 7.0.5)
- Microsoft.NET.Test.Sdk (>= 17.6.2)
- Newtonsoft.Json (>= 13.0.3)
- NUnit (>= 3.13.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.