/** * 05 — Display-currency switching (RUB / USD / EUR / KZT / UAH / TRY / …) * * Catalog endpoints quote product prices in any of the supported ISO-4217 * currencies. RUB is the legacy default — a client that doesn't ask for * anything else keeps getting ruble prices, same as before SDK 0.27. * * Resolution order on the server (highest priority first): * 1. `?currency=USD` query param (per-call override) * 2. `X-Currency` request header (SDK injects when constructor has * `currency: "USD"` configured) * 3. RUB default * * Run with: * GAMECORE_API_KEY=gc_live_... bun run examples/05-currency-switching.ts */ import { GameCoreClient, type SdkCurrency } from "@gamecore-api/sdk"; const apiKey = process.env.GAMECORE_API_KEY!; const baseUrl = "https://api.gamecore-api.tech"; // Approach A — one client per currency. Simplest for a single-currency // storefront (e.g. a US store that always shows USD). const ru = new GameCoreClient({ apiKey, baseUrl, currency: "RUB" }); const usd = new GameCoreClient({ apiKey, baseUrl, currency: "USD" }); const gameSlug = "free-fire"; const ruProducts = await ru.catalog.getProducts(gameSlug); const usdProducts = await usd.catalog.getProducts(gameSlug); console.log("RUB first product:", { name: ruProducts[0]?.name, price: ruProducts[0]?.price, currency: ruProducts[0]?.currency, // "RUB" }); console.log("USD first product:", { name: usdProducts[0]?.name, price: usdProducts[0]?.price, currency: usdProducts[0]?.currency, // "USD" }); // Approach B — single client, switch at runtime. Wire this to a // storefront currency picker so the next product fetch comes back // already converted. const gc = new GameCoreClient({ apiKey, baseUrl }); console.log("default currency:", gc.getCurrency()); // undefined → RUB gc.setCurrency("KZT"); const kztProducts = await gc.catalog.getProducts(gameSlug); console.log( "KZT first product:", kztProducts[0]?.price, kztProducts[0]?.currency, ); // Approach C — per-call override. Admin tool example: a Russian // operator inspects an EN/EUR preview of a product without flipping // the rest of the session. const previewCurrencies: SdkCurrency[] = ["EUR", "TRY", "UAH"]; for (const ccy of previewCurrencies) { const products = await gc.catalog.getProducts(gameSlug, { currency: ccy }); console.log(`${ccy}:`, products[0]?.price, products[0]?.currency); } // Notes for AI agents copying this example: // • Don't try to convert prices yourself — the server applies live FX // rates from open.er-api.com and rounds per-currency (whole KZT, // 2-decimal EUR, etc.). Doing it client-side will drift. // • Always use `product.currency` for the display string instead of // hardcoding the requested code — the server is the source of // truth when an FX rate is missing. // • Checkout is unrelated: payment gateways still settle in RUB or // USD depending on the chosen `paymentMethod`. The display // currency is purely catalog-side until checkout currency support // ships.