Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Install the official openai package, keep your API key in the server environment, and initialize the SDK with new OpenAI(). For the actual image-generation call, use the current OpenAI Images API guide to confirm the method, model, options, and response shape: the setup documentation establishes how to initialize the Node.js client, but the available image references do not establish a complete, verified JavaScript generation example. Avoid guessing at that call or at the property containing the returned image.

Set up the Node.js SDK safely

OpenAI’s Developer quickstart supports server-side JavaScript, including Node.js. It shows installing the openai npm package, supplying the API key through the OPENAI_API_KEY environment variable, and creating a client with new OpenAI().

1. Create a project and install the package

In a new project directory, initialize npm if needed and install the SDK:

npm init -y
npm install openai

Use an ES module file such as setup.mjs. The .mjs extension lets Node.js interpret the example as an ES module without requiring a separate package setting.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

2. Set the API key outside your source code

Set OPENAI_API_KEY in the environment of the process that runs your application. For a one-off shell session, a Unix-like shell can use:

export OPENAI_API_KEY="your_api_key"
node setup.mjs

In PowerShell, set it for the current session with:

$env:OPENAI_API_KEY = "your_api_key"
node setup.mjs

Do not put a real key in browser JavaScript, commit it to a repository, or bake it into a client bundle. The SDK reads the environment variable automatically, so the application server can authenticate without embedding the secret in code.

3. Confirm that Node can load and initialize the SDK

This minimal program is runnable after installation and environment setup. It confirms that the package imports and the client initializes; it does not send an image-generation request.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import OpenAI from "openai";

const client = new OpenAI();
console.log("OpenAI client initialized:", Boolean(client));

Run it with node setup.mjs. If initialization fails because the key is missing, set OPENAI_API_KEY in the same shell or process environment used to start Node.

Use the current Images API guide for the generation call

Client setup is not the image-generation request. OpenAI’s quickstart demonstrates general SDK initialization and a text request; it does not establish the image-generation method signature or the location of the generated image data in a non-streaming JavaScript response. The available image references also do not provide a complete runnable JavaScript generation example. For that reason, a method name or response property should not be guessed here.

Before adding the call to your application, open OpenAI’s current official Images API guide and verify these items for the endpoint and model you intend to use:

  • The SDK method and endpoint to call, and whether the guide’s example is for JavaScript or another language.
  • The model identifier currently available to your account.
  • The accepted request fields, their defaults, and any model-specific restrictions.
  • How the chosen endpoint returns the image: for example, whether you receive encoded image data or another form of result, and the documented response property containing it.
  • Any current account, organization, or project requirements that apply to image generation.

Do not substitute the text example in the quickstart for an image request. A successful client initialization only proves that Node loaded the SDK and constructed a client; it does not prove that the key has permission to generate images or that a particular model and request are supported.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose output settings for the job

Output format, quality, and dimensions are request decisions, not settings to assume universally. The supplied API reference material lists PNG, WebP, and JPEG formats; low, medium, high, or automatic quality; and dimensions of 1024x1024, 1024x1536, 1536x1024, or automatic sizing. Check the current endpoint documentation and selected model before relying on any of these values: availability and defaults can vary by endpoint or model.

Format

Choose a format based on how the result will be used. PNG, WebP, and JPEG are listed choices in the reference material, but that is not a guarantee that every format is supported for every model or endpoint. Confirm the accepted value in the current request schema, then save or serve the returned bytes with a matching content type and file extension.

Quality

The listed choices are low, medium, high, and auto. Use the documented option appropriate to the endpoint. Do not infer a specific quality-to-cost or quality-to-latency trade-off from these labels; the available material does not quantify one.

Dimensions

The listed fixed sizes are square, portrait, and landscape: 1024x1024, 1024x1536, and 1536x1024, respectively, alongside auto. If a particular aspect ratio matters, check that the chosen endpoint and model accept it before sending a request. Do not assume that an arbitrary width and height will work.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Decide whether streaming is useful

Streaming is a separate consideration from simply requesting an image. The Image Streaming API reference describes image-generation events that can contain base64-encoded image data, including completed image events. Streaming can suit an interface that needs to react to events as generation proceeds, but the event names, lifecycle, and JavaScript handling must come from the current endpoint guide.

The reference material does not establish a complete JavaScript event loop, decoder, or exact event payload path. Verify those details before writing code that parses streamed events or saves their image data. Do not assume that a streaming event has the same shape as a non-streaming response.

Check model availability and data-retention requirements

The model catalog describes GPT Image 1 as a state-of-the-art image-generation model and GPT Image 1 mini as a cost-efficient version. Those descriptions are not a guarantee of current availability for every account, nor do they establish a complete comparison of capability, price, or latency. Check the live catalog and endpoint documentation when selecting a model.

OpenAI’s data-controls documentation specifically identifies image generation with gpt-image-1 and gpt-image-1-mini as Zero Data Retention compatible, and says DALL·E 2 and DALL·E 3 are not. Treat this as a model-specific compatibility statement, not a claim that every form of API data handling is covered by Zero Data Retention. If retention requirements affect your deployment, confirm the current policy and your organization’s settings before choosing a model.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Troubleshoot the integration

Node reports that the package cannot be found

Confirm that npm install openai completed in the project directory from which you run Node. If the application runs in a container or deployment environment, install the dependency in that environment too.

The client cannot find an API key

Check that the variable is named exactly OPENAI_API_KEY and is set in the same process environment used to launch Node. Setting it in a different terminal, shell profile, or hosting configuration will not automatically update an already-running process.

The request rejects a model or an option

Verify the model name, endpoint, output format, quality, dimensions, and other fields against the current documentation for that exact endpoint. A choice listed for an image API is not necessarily accepted by every model or request path.

The request succeeds but your application cannot use the image

Inspect the documented response for the endpoint you called. A streaming payload and a non-streaming response need not have the same structure. Confirm whether the returned data is encoded or otherwise represented, decode it only as documented, and write it using the expected binary-handling path rather than treating image bytes as ordinary text.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Your application needs a streaming result

Use the streaming reference for the image event model, then verify the current Node.js example for subscribing to and handling those events. Do not reuse a text-streaming parser or infer the image event’s payload property from a different endpoint.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Performance, reliability, and cost considerations

The available official material does not establish a numerical cost or latency comparison for the models, quality levels, formats, or streaming choices discussed here. Avoid planning around an assumed generation time or per-image price. Consult the current model and API pricing information before estimating operating costs.

For reliability, keep the API call on a server you control, handle request failures at the application boundary, and avoid treating a constructed SDK client as proof that a generation request will succeed. Log enough operational context to diagnose failures without logging API keys or other secrets. If you add retries, follow the current API guidance and your application’s tolerance for repeated generation; the material cited here does not establish a universal retry policy.

Streaming may let an application process documented events before the full operation is complete, while a non-streaming flow is simpler when the caller only needs the completed result. Choose based on the behavior your interface needs, and implement the response shape documented for that endpoint.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Or skip the browser setup

ScreenshotNeo is for taking website screenshots, not generating images. If your Node.js application also needs a website screenshot, its API can return an image or PDF from one GET request. See the ScreenshotNeo documentation for parameters and response details.

const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);

ScreenshotNeo removes cookie banners, newsletter popups, and chat widgets before the shot; bot checks, blank pages, and failed loads are never billed; and an MCP server lets AI agents take screenshots. The Free plan includes 1,000 screenshots per month with no card, and paid plans start at $5 for 3,000. Sign up for 1,000 free screenshots a month, with no card required.

Frequently Asked Questions

Can this SDK example generate an image without a browser?

Yes. The OpenAI SDK is a server-side Node.js package; image generation is an API request, not a browser-rendering task. The exact request must match the current Images API documentation.

Does the SDK itself determine which image models my account can use?

No. The package initializes a client, while model availability and endpoint support are determined by OpenAI’s current API and account availability.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.