<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: PayAgents</title>
    <description>The latest articles on DEV Community by PayAgents (@payagents).</description>
    <link>https://dev.to/payagents</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4272113%2F7870ba1a-7986-4bbd-af8e-abfac6f07ec3.png</url>
      <title>DEV Community: PayAgents</title>
      <link>https://dev.to/payagents</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/payagents"/>
    <language>en</language>
    <item>
      <title>How an AI Agent Pays for an API Call with L402 (HTTP 402 and Lightning)</title>
      <dc:creator>PayAgents</dc:creator>
      <pubDate>Sun, 29 Nov 2026 11:15:59 +0000</pubDate>
      <link>https://dev.to/payagents/how-an-ai-agent-pays-for-an-api-call-with-l402-http-402-and-lightning-3a4p</link>
      <guid>https://dev.to/payagents/how-an-ai-agent-pays-for-an-api-call-with-l402-http-402-and-lightning-3a4p</guid>
      <description>&lt;p&gt;A paid API usually expects a person to be there: create an account, add a payment method, generate an API key. An agent running on its own needs another way to pay.&lt;/p&gt;

&lt;p&gt;HTTP reserved the status code &lt;code&gt;402 Payment Required&lt;/code&gt; for future use. L402 is one protocol that uses it, and x402 is another. Here is what happens, step by step, when an agent pays for a single API call over L402.&lt;/p&gt;

&lt;h2&gt;
  
  
  The four-step handshake
&lt;/h2&gt;

&lt;p&gt;L402 (originally called LSAT) lets a server charge for an HTTP resource over the Bitcoin Lightning Network. Lightning Labs documents the &lt;a href="https://docs.lightning.engineering/the-lightning-network/l402/protocol-specification" rel="noopener noreferrer"&gt;protocol specification&lt;/a&gt;. The payment itself is part of the credential.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Request.&lt;/strong&gt; The agent calls the paid endpoint like any other URL.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Challenge.&lt;/strong&gt; The server replies &lt;code&gt;402 Payment Required&lt;/code&gt; with a &lt;code&gt;WWW-Authenticate: L402&lt;/code&gt; header. It carries a macaroon (a token) and a Lightning invoice.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pay.&lt;/strong&gt; The agent pays the invoice. Paying reveals the invoice's preimage.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Retry.&lt;/strong&gt; The agent repeats the request with &lt;code&gt;Authorization: L402 &amp;lt;macaroon&amp;gt;:&amp;lt;preimage&amp;gt;&lt;/code&gt;. If the server can validate the pair for that service, it returns the data.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here is the shape of an exchange, with values shortened. It is an illustration, not output from a live service:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="no"&gt;GET&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;v2&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;forecast&lt;/span&gt;

&lt;span class="mi"&gt;402&lt;/span&gt; &lt;span class="nc"&gt;Payment&lt;/span&gt; &lt;span class="nc"&gt;Required&lt;/span&gt;
&lt;span class="na"&gt;WWW-Authenticate: L402 macaroon&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"AgEEbGF..."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;invoice&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"lnbc300n1p..."&lt;/span&gt;

&lt;span class="c1"&gt;# the client pays the invoice over Lightning and gets the preimage
&lt;/span&gt;
&lt;span class="no"&gt;GET&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;v2&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;forecast&lt;/span&gt;
&lt;span class="nc"&gt;Authorization&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;L402&lt;/span&gt; &lt;span class="nc"&gt;AgEEbGF&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;8f3c1a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

&lt;span class="mi"&gt;200&lt;/span&gt; &lt;span class="no"&gt;OK&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What this changes for agents
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;No separate account or API key per endpoint.&lt;/strong&gt; In the protocol exchange itself, the agent needs a way to pay Lightning invoices and a spending limit, not a signup at each service.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Payment and credential are linked.&lt;/strong&gt; The preimage that proves payment goes into the credential the server checks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Credentials can be reused.&lt;/strong&gt; Per the spec, an L402 credential is meant to be reused until the server revokes it, for example on expiry or after a usage limit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Treat the credential as a secret.&lt;/strong&gt; The macaroon and preimage travel in a header, and the spec says the scheme should be used over TLS. Anyone who gets a valid pair can use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  L402 or x402?
&lt;/h2&gt;

&lt;p&gt;x402 uses a similar 402 handshake but settles in stablecoins. PayAgents supports USDC on Base for x402.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;With Lightning, there is no on-chain transaction per payment, which is why it is often chosen for low-value payments. Routing fees and liquidity still apply, and the price of sats moves against fiat.&lt;/li&gt;
&lt;li&gt;x402 with USDC is dollar-denominated, which is simpler to budget. Each payment settles on a chain, so cost and confirmation time depend on that chain.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Which one fits depends on the price of each call, the currency you want to hold, and what your wallet supports. An endpoint also has to speak one of the two challenge formats. A bare 402 status is not enough.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where PayAgents fits
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://payagents.io/l402.html?utm_source=devto&amp;amp;utm_medium=article&amp;amp;utm_campaign=l402-tutorial" rel="noopener noreferrer"&gt;PayAgents&lt;/a&gt; is a payment layer for AI agents that works with both L402 and x402. On the buyer side, an agent pays APIs and MCP tools per request from a pre-funded PayAgents balance, within spending limits you set. On the seller side, you price an API, data or MCP tool per request. Payments can go to your own Lightning or Base address, or to a managed balance, which is the default.&lt;/p&gt;

&lt;p&gt;It is free while in beta. Using it as a buyer means a PayAgents account and a funded balance, so "no account" above describes the protocol exchange between agent and endpoint, not everything around it.&lt;/p&gt;

&lt;p&gt;This post is adapted from our longer guide, &lt;a href="https://payagents.io/l402.html?utm_source=devto&amp;amp;utm_medium=article&amp;amp;utm_campaign=l402-tutorial" rel="noopener noreferrer"&gt;What is L402?&lt;/a&gt;, which covers the macaroon format, the history of HTTP 402 and a fuller L402 vs x402 comparison. You can also try PayAgents at &lt;a href="https://payagents.io/?utm_source=devto&amp;amp;utm_medium=article&amp;amp;utm_campaign=l402-tutorial" rel="noopener noreferrer"&gt;payagents.io&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What would you want your agent to pay for first?&lt;/p&gt;

</description>
      <category>ai</category>
      <category>bitcoin</category>
      <category>api</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
