Skip to main content
net9.0

x402 Payment Required Coinbase facilitator client for .net

Published
Oct 7, 2025
1y ago
Total Downloads
4.9K
4,934

Install

dotnet add package x402.Coinbase --version 0.0.3
ProductVersions
.NET
net9.0

README

x402-dotnet

x402 Payment Protocol implementation for .Net

Install the x402 package from NuGet:

Features:

  • Add a x402 compatible paywall to any URL
  • Handle payment settlement using a remote server

How to use?

Setup the Facilitator in Program.cs

// Add the facilitator in Program.cs
builder.Services.AddHttpClient<IFacilitatorClient, HttpFacilitatorClient>(client =>
{
    client.BaseAddress = new Uri("https://localhost:7141"); // Address of your facilitator
});

Use the MVC Attribute

// Use the Payment Required Attribute
 [HttpGet]
 [Route("protected")]
 [PaymentRequired("1", "0x00..Asset..Address", "0xYourWalletAddressHere", "base-sepolia")]
 public IActionResult Protected()
 {
     return Content("Protected");
 }

Directly in an API Controller

[HttpGet]
[Route("dynamic")]
public async Task<IActionResult> Dynamic(string amount)
{
    var request = this.HttpContext.Request;
    var fullUrl = $"{request.Scheme}://{request.Host}{request.Path}{request.QueryString}";

    var x402Result = await X402Handler.HandleX402Async(this.HttpContext, facilitator, fullUrl,
        new Models.PaymentRequirements
        {
            Asset = "USDC",
            Description = "Dynamic payment",
            Network = "base-sepolia",
            MaxAmountRequired = amount,
            PayTo = "0x"
        });

    if (!x402Result.CanContinueRequest)
    {
        return new EmptyResult(); // Response already written by HandleX402Async, so just exit
    }

    //Continue with the request
}

Or use the Middleware to require payment for a list of URLs

// Add Middleware
var facilitator = app.Services.GetRequiredService<IFacilitatorClient>();
var paymentOptions = new x402.Models.PaymentMiddlewareOptions
{
    Facilitator = facilitator,
    DefaultPayToAddress = "0xYourWalletAddressHere", // Replace with your actual wallet address
    DefaultNetwork = "base-sepolia",
    PaymentRequirements = new Dictionary<string, x402.Models.PaymentRequirementsConfig>()
        {
            {  "/url-to-pay-for", new x402.Models.PaymentRequirementsConfig
                {
                    Scheme = x402.Enums.PaymentScheme.Exact,
                    MaxAmountRequired = 1000000,
                    Asset = "0x......", // Contract address of asset
                    MimeType = "application/json"
                }
            }
        },
};

app.UsePaymentMiddleware(paymentOptions);

Coinbase Facilitator

To use the Coinbase Facilitator, install x402.Coinbase

// Add the Coinbase Config and Facilitator
builder.Services.Configure<CoinbaseOptions>(builder.Configuration.GetSection(nameof(CoinbaseOptions)));
builder.Services.AddHttpClient<IFacilitatorClient, CoinbaseFacilitatorClient>();

Add to appsettings.json:

 "CoinbaseOptions": {
   "ApiKeyId": "YOUR_COINBASE_API_KEY_ID",
   "ApiKeySecret": "YOUR_COINBASE_API_KEY_SECRET"
 }

Facilitators

List of facilitators you can use:

Development

There is a sample website and mock Settlement server included.

  • Start the Aspire project: x402-dotnet.AppHost
  • Navigate to the sample website https://localhost:7154/
  • Use the x402.SampleWeb.http for sample web requests

Contributions

Contributions are welcome. Fork this repository and send a pull request if you have something useful to add.

Links

Useful tools when developing x402 solutions:

Changes from Previous Version

No structural changes between v0.0.2 and v0.0.3.

Adoption Assessment

Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.

Compatibility metadata lists 1 target framework; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.