--- url: https://developer-portal.gainsight.com/docs/sdk/examples.md description: >- Runnable code examples for the SDK — connector requests, headers and timeouts, error fallbacks, and Web SDK user context recipes --- # SDK Examples Ready-to-use snippets for common SDK tasks. Each example is self-contained — copy it into your widget or sitewide script and adapt it. ::: info Prerequisites These examples run in a widget, or in a sitewide script registered with `"type": "module"`, on a community page. Examples that touch `document` are written for sitewide scripts; inside a widget, query your own markup with `sdk.$()` in `init(sdk)` instead. Each imports `connectors` or `web` from `@gainsight-hub/sdk` — see [SDK](/sdk/) for setup and [SDK Reference](/sdk/reference) for every method and option. ::: ## Connectors ### GET request with query parameters Fetch weather data by passing query parameters to the connector: ```javascript import { connectors } from "@gainsight-hub/sdk"; const weather = await connectors.execute({ permalink: "weather-api", method: "GET", queryParams: { q: "Warsaw", units: "metric" } }); console.log(weather); ``` ### POST request with JSON body Create a resource by sending a JSON payload: ```javascript import { connectors } from "@gainsight-hub/sdk"; const result = await connectors.execute({ permalink: "crm-contacts", method: "POST", payload: { firstName: "Jane", lastName: "Doe", email: "jane@example.com" } }); console.log(result); ``` `PUT`, `PATCH`, and `DELETE` use the same call: change `method`, and identify the resource with `queryParams` or `pathParams`. ### Custom headers per request Pass additional headers for a single request without changing the client configuration: ```javascript import { connectors } from "@gainsight-hub/sdk"; const data = await connectors.execute({ permalink: "internal-api", method: "GET", headers: { "X-Request-Id": crypto.randomUUID(), "Accept-Language": "en-US" } }); ``` ### Client-wide headers and timeout `configure` changes the shared client, so its settings apply to every widget on the page: ```javascript import { connectors } from "@gainsight-hub/sdk"; connectors.configure({ headers: { "X-Client": "custom-widget" }, timeout: 60000 }); ``` ### Handling errors with a fallback Wrap `execute` calls in `try`/`catch` and render a fallback so the widget degrades gracefully instead of showing an empty container. This example lives inside `init(sdk)`: ```javascript import { connectors } from "@gainsight-hub/sdk" export async function init(sdk) { await sdk.whenReady() const container = sdk.getContainer() container.innerHTML = `