<?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: Byteforge</title>
    <description>The latest articles on DEV Community by Byteforge (@byteforgeboilerplates).</description>
    <link>https://dev.to/byteforgeboilerplates</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%2F4209701%2F2d67126d-c9bb-47fc-8d55-5f5fc76410bb.jpg</url>
      <title>DEV Community: Byteforge</title>
      <link>https://dev.to/byteforgeboilerplates</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/byteforgeboilerplates"/>
    <language>en</language>
    <item>
      <title>Signing webhooks so your receivers can actually trust them</title>
      <dc:creator>Byteforge</dc:creator>
      <pubDate>Fri, 30 Oct 2026 22:20:37 +0000</pubDate>
      <link>https://dev.to/byteforgeboilerplates/signing-webhooks-so-your-receivers-can-actually-trust-them-16dd</link>
      <guid>https://dev.to/byteforgeboilerplates/signing-webhooks-so-your-receivers-can-actually-trust-them-16dd</guid>
      <description>&lt;p&gt;Written for: developers who send or receive webhooks and want a simple, verifiable scheme.&lt;/p&gt;

&lt;p&gt;Hey, fellow developer. Webhooks are easy to send and surprisingly easy to fake. Anyone who knows your receiver's URL can POST to it. So the sender needs to sign each message, and the receiver needs to check that signature before doing anything. This post shows how this &lt;a href="https://byteforge.whop.site/" rel="noopener noreferrer"&gt;boilerplate&lt;/a&gt; does it, with a few excerpts from the production module. A stripped-down, runnable version with tests is in the &lt;a href="https://github.com/Byteforge-boilerplates/webhook-signing-sample" rel="noopener noreferrer"&gt;webhook-signing-sample&lt;/a&gt; repo. It uses &lt;code&gt;X-Example-Signature&lt;/code&gt; instead of the production header name, so the code in the sample won't match the excerpts exactly.&lt;/p&gt;

&lt;h2&gt;
  
  
  The header
&lt;/h2&gt;

&lt;p&gt;Every delivery carries one header:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;X-CompanyName-Signature: t=&amp;lt;unix&amp;gt;,v1=&amp;lt;hex&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Two parts matter. &lt;code&gt;t&lt;/code&gt; is the timestamp when the message was sent. &lt;code&gt;v1&lt;/code&gt; is an HMAC-SHA256 of the timestamp and the raw body, joined with a dot. Signing the timestamp is what lets receivers reject old messages.&lt;/p&gt;

&lt;h2&gt;
  
  
  Signing
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;_digest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;raw_body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timestamp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;hmac&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;timestamp&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;raw_body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hashlib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sha256&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;hexdigest&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;raw_body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timestamp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Signature header value ``t=&amp;lt;unix&amp;gt;,v1=&amp;lt;hex&amp;gt;`` for ``raw_body`` sent at ``timestamp``.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;t=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;timestamp&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;,v1=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;_digest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;raw_body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timestamp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details are easy to get wrong. The signature covers the raw bytes you sent, not the parsed JSON, so re-serializing the body on either side will break the check. And the timestamp is part of the signed string, so an attacker can't reuse a valid signature with a new time.&lt;/p&gt;

&lt;p&gt;Secrets look like &lt;code&gt;whsec_&lt;/code&gt; followed by 32 random hex characters. The module also computes a short, non-secret prefix for display, so the UI can identify a secret without revealing it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Verifying
&lt;/h2&gt;

&lt;p&gt;The receiver's side is &lt;code&gt;verify&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;time&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;timestamp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;tolerance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;span class="n"&gt;expected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;_digest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;raw_body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timestamp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;secret&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;secrets&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;constant_time_equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sig&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;exp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;sig&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;signatures&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;exp&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three things happen here. First, the timestamp must be within 300 seconds of the receiver's clock, which stops replays of old captures. Second, the function accepts a list of secrets, not just one. Third, the comparison uses &lt;code&gt;constant_time_equals&lt;/code&gt;, so an attacker can't learn the signature one character at a time from response timing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rolling secrets without downtime
&lt;/h2&gt;

&lt;p&gt;Secrets get rotated, and you don't want a deploy to drop messages. The header comment describes the approach: during a roll, deliveries are signed with the new secret, while verification accepts any secret still inside its overlap window. That's why &lt;code&gt;verify&lt;/code&gt; takes a list. Receivers that already have the new secret keep working, and receivers that still have the old one keep working until the window closes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Putting it together
&lt;/h2&gt;

&lt;p&gt;Here is the shape of a delivery, using the same helper the sender uses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;headers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;delivery_headers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;event_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;event_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;raw_body&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;raw_body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;timestamp&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()),&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It returns the content type, a user agent, the event ID, and the signature header. Setting &lt;code&gt;test=True&lt;/code&gt; adds a header that marks the call as a test, so receivers can tell test traffic from live traffic.&lt;/p&gt;

&lt;h2&gt;
  
  
  A limit to know about
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;verify&lt;/code&gt; does not remember which signatures it has already accepted. Within the 300-second window, a captured message can be replayed and will still pass. For that reason, receivers should deduplicate on the &lt;code&gt;X-CompanyName-Event-Id&lt;/code&gt; header and treat repeated events as no-ops. The signature stops forgery; deduplication stops replay.&lt;/p&gt;

&lt;p&gt;If you want the full webhook pipeline, delivery retries, and the rest of the backend, the boilerplate includes it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Get the full boilerplate: &lt;a href="https://byteforge.whop.site/" rel="noopener noreferrer"&gt;Byteforge&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>saas</category>
      <category>webdev</category>
      <category>security</category>
      <category>eventdriven</category>
    </item>
    <item>
      <title>Pricing metered API data to the cent without losing a single fraction</title>
      <dc:creator>Byteforge</dc:creator>
      <pubDate>Tue, 27 Oct 2026 01:56:07 +0000</pubDate>
      <link>https://dev.to/byteforgeboilerplates/pricing-metered-api-data-to-the-cent-without-losing-a-single-fraction-37e8</link>
      <guid>https://dev.to/byteforgeboilerplates/pricing-metered-api-data-to-the-cent-without-losing-a-single-fraction-37e8</guid>
      <description>&lt;p&gt;Written for: developers building usage-based billing who want to see how one backend handles it.&lt;/p&gt;

&lt;p&gt;Hey, fellow developer. If you've ever built "pay for what you use" billing, you've probably hit the same wall I did: you charge per gigabyte, every call is a few kilobytes, and suddenly your rounding is eating money. This post walks through how the backend in this &lt;a href="https://byteforge.whop.site/" rel="noopener noreferrer"&gt;boilerplate&lt;/a&gt; prices metered API data so every cent is accounted for, with a short look at the code.&lt;/p&gt;

&lt;p&gt;I'll keep the code excerpts small. A stripped-down, runnable version of the pricing module with unit tests is in the &lt;a href="https://github.com/Byteforge-boilerplates/metered-pricing-sample" rel="noopener noreferrer"&gt;metered-pricing-sample&lt;/a&gt; repo, so you can try the math yourself. The excerpts below are from the production module.&lt;/p&gt;

&lt;h2&gt;
  
  
  The billable metric
&lt;/h2&gt;

&lt;p&gt;Before any math, you need to decide what you're counting. Here the metric is request body bytes plus response body bytes, and only for live-key calls that were answered with a status below 500. Test-key calls are free. Errors from our side don't cost the customer anything.&lt;/p&gt;

&lt;p&gt;The included allowance comes first. Each plan has a number of bytes that's covered by the subscription. Only the bytes beyond that allowance are priced by the overage tiers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why rounding is the hard part
&lt;/h2&gt;

&lt;p&gt;Say overage costs 600 cents per GiB. A single API call might be 4 KB. Its cost is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;4 KB × 600 cents / 1 GiB ≈ 0.0023 cents&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you round each call on its own, you'll round every call to zero, and you'll never bill anything. If you round up each call, you'll overcharge by a huge margin. Neither approach is correct.&lt;/p&gt;

&lt;p&gt;The trick is to stop pricing each call in isolation. Price the cycle's total, then charge each call the difference. Look at this excerpt from &lt;code&gt;pricing.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;cost_cents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;billable_bytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;PricingTerms&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;``f``: the cycle&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s overage cost in cents at ``billable_bytes``, rounded half up once. A
    discount scales the exact numerator, so it is applied before the single rounding.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;kept&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;discount_percent&lt;/span&gt;
    &lt;span class="nf"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;overage_numerator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;billable_bytes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;kept&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;GB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;GB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;debit_cents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;settled_bytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;call_bytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;PricingTerms&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Cents to debit for a call of ``call_bytes`` settled at cumulative ``settled_bytes`` (&amp;gt;= 0).&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;cost_cents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;settled_bytes&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;call_bytes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nf"&gt;cost_cents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;settled_bytes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each call is debited &lt;code&gt;f(S + call) - f(S)&lt;/code&gt;, where &lt;code&gt;S&lt;/code&gt; is the cycle's running total. When you add up all the debits in a cycle, they telescope to &lt;code&gt;f(S_end)&lt;/code&gt;, so the total is exact, and the fractions of a cent carry forward instead of disappearing. The cycle's byte count is what moves; the cents follow from it.&lt;/p&gt;

&lt;h2&gt;
  
  
  A worked example
&lt;/h2&gt;

&lt;p&gt;Take a Standard-style setup: 50 GiB included, overage at 600 cents per GiB, no discount. Suppose a cycle has used exactly 51 GiB. The overage is 1 GiB:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The numerator is &lt;code&gt;1 GiB × 600 = 600·GiB&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Multiply by 100 (the &lt;code&gt;kept&lt;/code&gt; factor with no discount): &lt;code&gt;60000·GiB&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;50·GiB&lt;/code&gt; for the half-up rounding, then divide by &lt;code&gt;100·GiB&lt;/code&gt;: &lt;code&gt;60050 / 100 = 600&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the cycle costs 600 cents, which is $6.00. That number is exact, because there's no fractional cent left over.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tiers
&lt;/h2&gt;

&lt;p&gt;Tiers let you price the first chunk of overage at one rate and later chunks at a lower one. The loop in &lt;code&gt;overage_numerator&lt;/code&gt; walks through them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;tier&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tiers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;overage&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;
    &lt;span class="n"&gt;upper&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;overage&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;tier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up_to_bytes&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;overage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up_to_bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;upper&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;tier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cents_per_gb&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;tier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up_to_bytes&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;
    &lt;span class="n"&gt;lower&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up_to_bytes&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each tier only charges for the bytes that fall inside it. The last tier has no upper bound. The module also has a &lt;code&gt;validate_tiers&lt;/code&gt; function that rejects tier lists which aren't ordered correctly, so a bad tier list can be caught before it's used to bill anyone.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keep the numbers in one place
&lt;/h2&gt;

&lt;p&gt;The useful habit here is that the cent-level math lives in one pure module. It has no database calls, no settings lookups, and no I/O, so you can test it with plain integers. Use integers for money too, never floats.&lt;/p&gt;

&lt;p&gt;If you want the whole flow (holds, prepaid balances, free trials, and arrears), the boilerplate includes it. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Get the full boilerplate: &lt;a href="https://byteforge.whop.site/" rel="noopener noreferrer"&gt;Byteforge&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>backend</category>
      <category>api</category>
      <category>tutorial</category>
      <category>saas</category>
    </item>
  </channel>
</rss>
