<?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: Химяк Павел</title>
    <description>The latest articles on DEV Community by Химяк Павел (@partna).</description>
    <link>https://dev.to/partna</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%2F2813199%2F3e4ca73d-306f-4e6d-b120-86e3694817f5.png</url>
      <title>DEV Community: Химяк Павел</title>
      <link>https://dev.to/partna</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/partna"/>
    <language>en</language>
    <item>
      <title>Your frontend should not talk to your partner API</title>
      <dc:creator>Химяк Павел</dc:creator>
      <pubDate>Wed, 18 Nov 2026 16:13:05 +0000</pubDate>
      <link>https://dev.to/partna/your-frontend-should-not-talk-to-your-partner-api-402o</link>
      <guid>https://dev.to/partna/your-frontend-should-not-talk-to-your-partner-api-402o</guid>
      <description>&lt;p&gt;An affiliate integration usually starts the same way. The partner API needs a tenant key, the site&lt;br&gt;
needs to call the API, and the quickest path is to put the key in the frontend and call it from&lt;br&gt;
there. It works. It also ships three problems that show up months later, in production, at the worst&lt;br&gt;
possible moment.&lt;/p&gt;

&lt;p&gt;I built a small service to avoid all three and released it as MIT: &lt;code&gt;partna-bridge&lt;/code&gt;. But the&lt;br&gt;
interesting part is not the code — it is why the naive integration breaks.&lt;/p&gt;
&lt;h2&gt;
  
  
  Failure one: the key is in the browser
&lt;/h2&gt;

&lt;p&gt;Open the network tab on almost any small affiliate programme and you will find an API key, a project&lt;br&gt;
token or a signed URL in the JavaScript bundle. Anyone can copy it, then call your partner API and&lt;br&gt;
award themselves a commission.&lt;/p&gt;

&lt;p&gt;The fix is not "hide the key a bit better". A key that reaches the browser is public, full stop. The&lt;br&gt;
only real option is a server-side proxy that holds the key in an environment variable and never&lt;br&gt;
returns it in a response.&lt;/p&gt;
&lt;h2&gt;
  
  
  Failure two: attribution lives in a tab
&lt;/h2&gt;

&lt;p&gt;The second failure is quieter. Most implementations store the referral code in &lt;code&gt;sessionStorage&lt;/code&gt; or in&lt;br&gt;
a JavaScript variable. That survives navigation, not a closed tab. A visitor clicks a partner link,&lt;br&gt;
looks around, closes the page, comes back a week later and buys. The partner gets nothing, and the&lt;br&gt;
partner is right to be annoyed.&lt;/p&gt;

&lt;p&gt;What you want is a cookie on the customer's own domain: &lt;code&gt;HttpOnly&lt;/code&gt;, &lt;code&gt;SameSite=Lax&lt;/code&gt;, with &lt;code&gt;Secure&lt;/code&gt; set&lt;br&gt;
when the public URL is https, and a lifetime measured in weeks rather than minutes. The code then&lt;br&gt;
travels with the browser, not with one page view.&lt;/p&gt;
&lt;h2&gt;
  
  
  Failure three: webhooks everywhere
&lt;/h2&gt;

&lt;p&gt;The engine needs to tell the site that a payment or a refund happened. If every service in the&lt;br&gt;
project accepts that webhook, signature verification gets implemented three times, in three styles,&lt;br&gt;
and one of them inevitably skips the check.&lt;/p&gt;

&lt;p&gt;One public endpoint that verifies the signature over the raw body, deduplicates by event id and&lt;br&gt;
answers &lt;code&gt;202&lt;/code&gt; is easier to reason about than three handlers that disagree.&lt;/p&gt;
&lt;h2&gt;
  
  
  What the bridge does
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;partna-bridge&lt;/code&gt; runs on the customer's server, in front of the engine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;visitor  --/r/CODE--&amp;gt;  bridge  --Set-Cookie--&amp;gt;  visitor
frontend --config, claim--&amp;gt;  bridge
backend  --order, refund, partner-link--&amp;gt;  bridge  --X-API-Key--&amp;gt;  engine
engine   --webhook, signed--&amp;gt;  bridge
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Money never passes through it. It is a proxy with a cookie, a queue and a signature check:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;GET /health&lt;/code&gt; — alive, and whether the tenant key is configured;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;GET /v1/config&lt;/code&gt; — brand, currency, destination URL for the frontend;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;GET /r/&amp;lt;code&amp;gt;&lt;/code&gt; — sets the binding cookie and redirects back with &lt;code&gt;?ref=&amp;lt;code&amp;gt;&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;POST /v1/claim&lt;/code&gt; — binds a client to a code, from the body or from the cookie;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;POST /v1/order&lt;/code&gt;, &lt;code&gt;POST /v1/refund&lt;/code&gt; — server-only, behind a bearer token;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;POST /webhooks/engine&lt;/code&gt; — engine events with &lt;code&gt;X-Partner-Signature&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The frontend must never be able to call &lt;code&gt;order&lt;/code&gt;. Otherwise you have rebuilt failure one with extra&lt;br&gt;
steps.&lt;/p&gt;
&lt;h2&gt;
  
  
  Running it
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/partna-dev/partna-bridge
&lt;span class="nb"&gt;cd&lt;/span&gt; partna-bridge
./scripts/install.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The script generates secrets, builds the container, waits for &lt;code&gt;/health&lt;/code&gt; and prints what is left to&lt;br&gt;
connect. Configuration is a handful of variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight conf"&gt;&lt;code&gt;&lt;span class="na"&gt;BRIDGE_ENGINE_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//networks.partna.dev&lt;/span&gt;
&lt;span class="na"&gt;BRIDGE_ENGINE_API_KEY&lt;/span&gt;&lt;span class="o"&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="c1"&gt;# server-side only&lt;/span&gt;
&lt;span class="na"&gt;BRIDGE_PUBLIC_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//app.example.com&lt;/span&gt;
&lt;span class="na"&gt;BRIDGE_DESTINATION_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//app.example.com&lt;/span&gt;
&lt;span class="na"&gt;BRIDGE_BRIDGE_TOKEN&lt;/span&gt;&lt;span class="o"&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="c1"&gt;# bearer token for order/refund&lt;/span&gt;
&lt;span class="na"&gt;BRIDGE_WEBHOOK_SECRET&lt;/span&gt;&lt;span class="o"&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="c1"&gt;# webhook signature&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Until a key is set, &lt;code&gt;/health&lt;/code&gt; returns &lt;code&gt;key_configured: false&lt;/code&gt; and protected endpoints answer &lt;code&gt;503&lt;/code&gt;.&lt;br&gt;
That is deliberate: a half-configured bridge should look broken, not behave unpredictably.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is actually tested
&lt;/h2&gt;

&lt;p&gt;Twelve tests, and they cover the things that break in production rather than the happy path:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the tenant key never appears in a response;&lt;/li&gt;
&lt;li&gt;the binding cookie is set, is &lt;code&gt;HttpOnly&lt;/code&gt;, and survives;&lt;/li&gt;
&lt;li&gt;an order without the bearer token is rejected;&lt;/li&gt;
&lt;li&gt;a webhook with a wrong signature is dropped;&lt;/li&gt;
&lt;li&gt;a repeated event id is marked as a duplicate.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The outbox deserves a note: &lt;code&gt;BRIDGE_OUTBOX_MODE=queue&lt;/code&gt; writes orders and refunds to SQLite and answers&lt;br&gt;
&lt;code&gt;202&lt;/code&gt;, then retries with backoff. &lt;code&gt;inline&lt;/code&gt; sends immediately and fails loudly. For a partner engine on&lt;br&gt;
the other side of the internet, the queue is the sane default — a customer's checkout should not fail&lt;br&gt;
because someone else's API hiccuped.&lt;/p&gt;

&lt;h2&gt;
  
  
  Honest limitations
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The processed event list lives in memory, so after a restart a repeated event passes again. The host
should still deduplicate.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;claim&lt;/code&gt; is rate-limited per address; a public site behind a proxy needs its own protection.&lt;/li&gt;
&lt;li&gt;It is not a billing system, not an analytics platform, and it will not design your commission plan.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Links
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Code: &lt;a href="https://github.com/partna-dev/partna-bridge" rel="noopener noreferrer"&gt;github.com/partna-dev/partna-bridge&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;The programme model behind it: &lt;a href="https://partna.dev" rel="noopener noreferrer"&gt;partna.dev&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Notes while building: &lt;a href="https://t.me/partnadev" rel="noopener noreferrer"&gt;t.me/partnadev&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>api</category>
      <category>security</category>
      <category>webdev</category>
      <category>python</category>
    </item>
  </channel>
</rss>
