# Custom HTTP clients

SDK users can provide a custom HTTP client when initializing SDKs. This is useful for modifying or debugging requests and responses in flight.

See below for per-language examples:

<CodeWithTabs client:visible
  tabs={[
    {
      label: "Go",
      language: "go",
        code: `/* The Go SDK will accept a client that implements a 
     \`Do(*http.Request) (*http.Response, error)\` method similar 
     to the standard library's \`http.Client\`. */

  // A custom HTTP client that implements caching
  c := NewCachedClient(&http.Client{}, cache)

  opts := []sdk.SDKOption{
      sdk.WithClient(c),
  }

  s := sdk.New(opts)`,
      },
    {
      label: "Python",
      language: "python",
      code: `# The Python SDK will accept any client that implements the 
  # \`HttpClient\` interface from the SDK. Here's an example using the 
  # [\`requests\`](https://requests.readthedocs.io/en/latest/) library:

// Create an HTTPClient instance with the default fetcher
const httpClient = new HTTPClient({
  // fetcher takes a function that has the same signature as native \`fetch\`.
  fetcher: async (request) => fetch(request),
});

httpClient.addHook("requestError", (err) => {
  console.log(\`Request failed: \${err}\`);
});

// Initialize the SDK with the custom HTTP client
const sdk = new SDK({ httpClient });`,
    },
    {
      label: "Java",
      language: "java",
      code: `/* The Java SDK will accept a client that implements the 
  \`HTTPClient\` interface in the \`utils\` package. This will wrap 
  a \`java.net.http.HttpClient\` instance and the call to \`send\`. */

  // Custom HTTP client
  YouHttpClient client = new YourHttpClient();

  SDK.Builder builder = SDK.builder();

  builder.setClient(client);

  SDK sdk = builder.build();`,
    },
    {
      label: "C#",
      language: "csharp",
      code: `/* YourHttpClient must implement the ISpeakeasyHttpClient interface */

var httpClient = new YourHttpClient();
// Initialize the SDK with the custom HTTP client
var sdk = new SDK(client: httpClient);`,
}
]}
/>
