Knowledge Base/How to use HTTP proxy with Uniform SDK and CLI

How to use HTTP proxy with Uniform SDK and CLI

how-toDeveloperCLISDKSecurity

Uniform SDK and CLI uses the endpoints below by default:

https://uniform.app

https://uniform.global

For REST API examples, you can use these resources:

https://uniform.app/schemas/swagger.html

https://uniform.global/

Sometimes, the developers machine or the environment for the production site may be behind a firewall without an explicit access to the internet. You can configure Uniform SDK and CLI to use a HTTP proxy endpoint.

There are 2 areas where it needs to be done. Please follow the steps for each one below.

Even if you don’t explicitly use Uniform CLI to pull or push data, it is still required for a NextJS page router app. On app start it downloads the Uniform Context manifest for personalization or A/B testing by default. The app start may fail without CLI proxy config

Set the HTTPS_PROXY or HTTP_PROXY environment variable with the proxy server IP and port like this: HTTPS_PROXY=http://192.167.10.8:8080

The steps below are different for NextJs page router and app router, due to different Uniform SDK used there. However, the app start should not fail with app router, because the manifest is pulled later.

For each Uniform SDK client, add the fetch parameter which pollyphils the default fetch with your implementation with the HTTP proxy. For example:

import { HttpsProxyAgent } from "https-proxy-agent"; function ProxyFetch(input: RequestInfo | URL, init?: RequestInit) { if (process.env.HTTP_PROXY) { console.log("Using Proxy"); // proxy required non local build const agent = new HttpsProxyAgent(process.env.HTTP_PROXY); const props = { agent, ...(init ?? {}) }; return fetch(input, props); } else { return fetch(input, init); } } export const routeClient = new RouteClient({ apiKey, projectId, fetch: ProxyFetch, });

Uniform had a fetch parameter for the SDK clients, which we used to use to set up http-proxy with page routr. Due to the nature of NextJS app router now, overriding it won't solve the issue. NextJS app router has its own fetch with more caching features, but do not provide a way to use an HTTP proxy. https://nextjs.org/docs/app/api-reference/functions/fetch https://github.com/vercel/next.js/discussions/45635

As a workaround, run an app locally which will translate Uniform SDK requests to your proxy. Here is the code example:
https://github.com/uniform-collab/proxy-sample
You need to run the app 2 times, and make sure both instances are always up and running. Set up the .env

HTTP_PROXY=http://10.0.0.113:9090 TARGET_HOSTNAME=https://uniform.app TARGET_HOSTNAME_GLOBAL=https://uniform.global

Run the apps (one command per a single CMD):

npm run dev-api npm run dev-global

Than set the Uniform environment variables in your main app to point to these new app endpoints, so any requests coming to /api/ will be proxied to your network proxy UNIFORM_API_HOST=http://local.app:8080 UNIFORM_EDGE_API_HOST=http://local.app:8081 The reason to use another proxy before it reaches your main network proxy is to replace the hostname with the correct uniform.app or uniform.global.

Last modified: January 6, 2025