Creating Images with Gemini and TypeScript
Introduction to Gemini and TypeScript
Welcome to the first lesson of our course, "Creating Images with Gemini and TypeScript". In this course, you will explore the fascinating world of AI-driven image generation using Google's Gemini API and its native image generation. Our journey begins with understanding how to set up the environment and generate a simple image. We'll also touch upon TypeScript, a typed superset of JavaScript that helps us write safer Node.js code. This foundational lesson will set the stage for more advanced topics in subsequent units.
Setting Up the TypeScript Environment
Before we begin generating images, it's essential to set up our TypeScript environment correctly. First, ensure you have Node.js installed on your system. You can verify this by running node -v and npm -v in your terminal. If it is not installed, download and install Node.js from the official website.
Next, initialize a new Node.js project and install the necessary packages. We'll use npm for package management. Create a new directory for your project and run the following commands:
Pin @types/node to your installed Node.js major version (Node 20 LTS above) and keep TypeScript in step.
The @google/genai package provides access to the Gemini API, and dotenv helps manage environment variables. Create a .env file in your project root to store your API key securely:
Next, add a tsconfig.json at the project root so the compiler knows how to build the project. We compile to CommonJS, which is what makes Node globals like __dirname (used later when saving the image) available at runtime:
Place your code in app/main.ts. Compile and run it with:
Configuring the Gemini API Client
With the environment set up, the next step is to configure the Gemini API client using TypeScript. We'll use the dotenv package to load environment variables and initialize the client with your API key. Here's how you can set up the client:
This setup ensures that your application can securely communicate with the Gemini API.

