APIs & Integrations

Third-Party SDKs

Third-party SDKs accelerate development β€” but they need careful integration. Learn the wrapper pattern, dynamic loading, feature flags, mock stubs for testing, and how to fail gracefully when an SDK is unavailable.

How do I add third-party SDKs safely?

Install SDKs (payments, analytics, maps, chat) from npm when possible, so they're versioned and bundled, or load their script asynchronously so it doesn't block your page. Initialise them only where they're needed, keep secret keys on your server, and lazy-load heavy widgets after user interaction. Every third-party script costs performance and adds risk.

// load only when needed
button.addEventListener("click", async () => {
  const { loadStripe } = await import("@stripe/stripe-js");
  const stripe = await loadStripe(PUBLISHABLE_KEY);
});

<script src="https://example.com/widget.js" async></script>

SDK Integration Patterns

πŸ”§

Wrapper / Adapter

Wrap the SDK in your own service class. Call your service everywhere β€” change the SDK in one place. Essential for testability.

⚑

Dynamic Loading

Load SDKs only when needed with dynamic import() or script injection. Reduces initial bundle size and improves page load.

πŸ“‹

Event Queue

Queue SDK calls before the SDK loads. Flush the queue on init. Prevents missed events during async initialization.

πŸ§ͺ

Mock Stub

Proxy object that logs calls instead of executing them. Use in unit tests and development without real API keys.

🚩

Feature Flags

Control SDK initialization by feature flag. Roll out to % of users, disable globally without redeploy.

πŸ›‘οΈ

Error Isolation

Wrap all SDK calls in try/catch. Never let a third-party error crash your app. Log warnings, degrade gracefully.

Common Pitfalls

Calling SDK methods directly all over the codebase β€” tight coupling to a specific vendor
Create a service/wrapper class. Your app code calls your service, never the SDK directly. Swap SDKs with one edit.
Loading all SDK scripts synchronously in <head> β€” blocks page render, hurts Core Web Vitals
Use async/defer attributes or dynamic script injection. Load analytics and chat widgets after page interactive.
Not handling SDK unavailability β€” SDK blocked by adblockers or network failures crashes the app
Wrap all SDK code in try/catch. Use optional chaining (sdk?.track()). Log warnings, never throw to the user.
Using real API keys in tests β€” slow, expensive, unpredictable, and can generate real charges
Create a mock stub with Proxy or jest.mock(). Use environment variables: test env uses mocks, prod uses real.

Interactive Demo

Code Editor

<button id="track">Load analytics SDK & track click</button>
<pre id="out"></pre>

Preview

Third-party SDKs: summary

Key points

  • Measure the cost of each SDK in the Network and Performance panels.
  • Use publishable or public keys in the browser and secret keys only on the server.
  • Wrap SDK calls in your own small module so it's easy to swap providers.
  • Respect consent requirements (GDPR) before loading tracking scripts.

Frequently asked questions

β€ΊWhy is my site slow after adding a widget?

Third-party scripts often download a lot of JavaScript and block the main thread. Load them with async or defer, delay them until interaction, or use a lighter alternative.