Welcome to the first lesson of our course, "Creating Images with Gemini 3.1 Flash and Go." In this course, you will explore AI-driven image generation using Google's Gemini API and its Gemini 3.1 Flash image model. Our journey begins with understanding how to set up the environment and generate a simple image using Go. This foundational lesson will set the stage for more advanced topics in subsequent units.
Before we dive into generating images, it's crucial to set up our environment correctly. First, ensure you have access to the Gemini API by retrieving your API key. This key is essential for authenticating your requests to the API. You can set this key in your environment variables. For this lesson, you'll need to use Go Modules, Go's package management system, to install the necessary libraries. You can use the net/http package for making HTTP requests.
Note that the environment will be fully set up for the practices section, so you can focus on the exercises without worrying about the setup.
You can get more details about how to use the Gemini Image Generation API from Google's documents. There, you can get the important details about connecting to the API, namely the GEMINI_BASE_URL and the GEMINI_API_KEY.
For example, when connecting directly to Gemini, the GEMINI_BASE_URL will be https://generativelanguage.googleapis.com/ and the GEMINI_API_KEY is your own secret. In all of our examples and practices, we will provide these variables to you so you don't need to be concerned with them for now.
Although Go client libraries exist for many Gemini workflows, this course uses the REST API directly for image generation so the request structure is clear and consistent with the practices.
We will later encapsulate this REST request behind a reusable helper function.
First, we need to retrieve the Base URL and API key from your environment variables. From there, we can construct an API request to the Image Generation model, which has a full URL of:
From there, we attach the API key as a request header and we have a fully constructed request:
This setup ensures that your application can securely communicate with the Gemini API.
Next, we can construct a JSON payload with our prompt. The minimal JSON payload will look like this:
With payload, we can POST to the URL we constructed:
Next, we need to extract the data out of the response. The data will come back as Base64 encoded JSON data.
Once the image is generated, the next step is to process and display it. Here's how you can handle the Base64 encoded image data and save it to a file:

If all of this seems too complicated, don't fret! We'll encapsulate it for you and most of the course will utilize a simple generation function called generateGeminiImages(). It's important to understand how the code works so you can replicate it in your own projects!
In this lesson, you learned how to set up your environment, configure the Gemini API, and generate a simple image using a prompt. We also explored how to process and save the generated image using Go. This foundational knowledge will be crucial as you progress through the course. As you move on to the practice exercises, I encourage you to experiment with different prompts and configurations to see the diverse range of images you can create. This hands-on practice will solidify your understanding and prepare you for more advanced topics in the upcoming lessons.
