This quickstart shows you how to get started with the Gemini API using the SDK of your choice.
Prerequisites
This quickstart assumes that you're familiar with building applications with Node.js.
To complete this quickstart, make sure that your development environment meets the following requirements:
- Node.js v18+
- npm
Install the Gemini API SDK
To use the Gemini API in your own application, you need to install the
GoogleGenerativeAI
package for Node.js:
npm install @google/generative-ai
Authenticate
Set up your API key
To use the Gemini API, you'll need an API key. If you don't already have one, create a key in Google AI Studio.
Then, configure your key.
It is strongly recommended that you do not check an API key into your version control system but assign it as an environment variable instead:
export API_KEY=<YOUR_API_KEY>
Initialize the model
Before you can make any API calls, you need to import and initialize the model. Gemini 1.5 models are versatile and work with both text-only and multimodal prompts.
const { GoogleGenerativeAI } = require("@google/generative-ai");
// Access your API key as an environment variable (see "Set up your API key" above)
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash"});
Make your first request
Generate text
async function run() {
const prompt = "Write a story about an AI and magic"
const result = await model.generateContent(prompt);
const response = await result.response;
const text = response.text();
console.log(text);
}
run();
What's next
Now that you're set up to make requests to the Gemini API, you can use the full range of Gemini API capabilities to build your apps and workflows. To get started with Gemini API capabilities, see the following guides:
- Text generation
- Vision
- Audio
- Long context
- Code execution
- JSON mode
- Model tuning
- Function calling
- System instructions
- Embeddings
- Safety settings
- Context caching
If you're new to working with generative AI or the Gemini API, check out the following guides, which will help you understand the Gemini API programming model:
For in-depth documentation of Gemini API methods and request parameters, see the guides in the API reference.