How do I upload an image to Cloudinary from behind a corporate proxy

My API uses the Cloudinary SDK to upload an image with the uploader.upload() method. But it times out.

I have the env variables set, http_proxy/https_proxy, that allow most everything else (such as the fetch API) to function.

As soon as I leave the corporate WiFi and thus the proxy, and remove all of the env variables, the upload works fine.

In this example, I have already set api_proxy in the cloudinary.config(), and send “proxy” with the options, but neither of those seem to do anything.

The GET method just confirms that there’s nothing wrong with my API route.

import { v2 as cloudinary } from "cloudinary";
import { NextResponse } from "next/server";


cloudinary.config({
  cloud_name: process.env.CLOUDINARY_NAME,
  api_key: process.env.CLOUDINARY_KEY,
  api_secret: process.env.CLOUDINARY_SECRET,
  api_proxy: "http://corp-proxy:1234",
});

export async function GET() {
  return NextResponse.json({ message: "Hello from upload" }, { status: 200 });
}

export async function POST(request: Request) {
  const { path } = await request.json();

  if (!path) {
    return NextResponse.json(
      { message: "Image path is required" },
      { status: 400 }
    );
  }

  try {
    const options = {
      use_filename: true,
      unique_filename: false,
      overwrite: true,
      transformation: [{ width: 1000, height: 752, crop: "scale" }],
      proxy: "http://corp-proxy:1234",
    };

    //cloudinary result after upload
    const result = await cloudinary.uploader.upload(path, options);

    return NextResponse.json(result, { status: 200 });
  } catch (error) {
    console.log("error uploading image", error);
    return NextResponse.json({ message: error }, { status: 500 });
  }

One more note: although most https calls work fine after I set the environment variables and the npm config to use the proxy, Google OAuth is an example of another one that fails unless I leave the network. I don’t know if there is a pattern there, but I can’t think of any other config that would force these APIs to use the proxy

Leave a Comment