<p>Minutes after posting, came up with the answer, why didn't I think of this solution before.</p>
<p>Rather than switching out API Keys, instead, I'm creating multiple payment gateways. Then conditionally loading the gateways in the template based on the site handle.</p>
<p>This has the added benefit of ensuring when we do refunds, it will refund against the correct gateway.</p>
<p>Would be nice if Payment Gateways could be associated with a store in the future, guess that will come with multi site support in Commerce 5.</p>

CC BY-SA 4.0
2022-04-11T17:25:16.173Z
2022-04-11T17:25:16.173Z
neekster
<p>I've got a multiple sites setup in Craft, with Craft Commerce running on top.</p>
<p>What I need to do is for one of the Payment Gateways, send a different API Key, based on the site that's being accessed.</p>
<p>My intent was to define each of the API Keys in the env config, so SITE_1_API_KEY etc, then add a conditional into the commerce-gateways.php config file, that would set a different env var based on the site handle being accessed.</p>
<p>I can't find any documented way of setting a conditional for this though, either by site handle or hostname.</p>

<multi-site><payment-methods>
How to set environment variables based site
# How to set environment variables based site

(Asked by: username_0 on 2022-04-11)

I've got a multiple sites setup in Craft, with Craft Commerce running on top.

What I need to do is for one of the Payment Gateways, send a different API Key, based on the site that's being accessed.

My intent was to define each of the API Keys in the env config, so SITE\_1\_API\_KEY etc, then add a conditional into the commerce-gateways.php config file, that would set a different env var based on the site handle being accessed.

I can't find any documented way of setting a conditional for this though, either by site handle or hostname.

**Tags:** multi-site, payment-methods


---

## 1 answers

### Answer by username_0 on 2022-04-11. Score: 1 | Accepted answer

Minutes after posting, came up with the answer, why didn't I think of this solution before.

Rather than switching out API Keys, instead, I'm creating multiple payment gateways. Then conditionally loading the gateways in the template based on the site handle.

This has the added benefit of ensuring when we do refunds, it will refund against the correct gateway.

Would be nice if Payment Gateways could be associated with a store in the future, guess that will come with multi site support in Commerce 5.


---

