ApiToolkit.Net 1.0.4

dotnet add package ApiToolkit.Net --version 1.0.4
NuGet\Install-Package ApiToolkit.Net -Version 1.0.4
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="ApiToolkit.Net" Version="1.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ApiToolkit.Net --version 1.0.4
#r "nuget: ApiToolkit.Net, 1.0.4"
#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.
// 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">

APItoolkit's Logo APItoolkit's Logo

.NET SDK

NuGet Build Status NuGet Nuget

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

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.

  1. RedactHeaders: A list of HTTP header keys (e.g., COOKIE (redacted by default), CONTENT-TYPE, etc.).
  2. RedactRequestBody: A list of JSONPaths from the request body (if the request body is a valid JSON).
  3. 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 the books field inside the store object with the string [CLIENT_REDACTED]).
  • $.store.books[*].author (In this case, APItoolkit will replace the author field in all the objects in the books list inside the store 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 and RedactResponseBody 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:

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 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. 
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
1.0.4 94 4/17/2024
1.0.3 200 5/8/2023
1.0.2 127 5/8/2023
1.0.1 114 5/8/2023
1.0.0 125 5/7/2023