A module to connect to gemini api.
npm install @izumiapi/gemini-apiš§ This package is currently in testing/maintenance phase š§
A simple and lightweight wrapper for Google's Gemini AI API that provides session-based chat functionality with automatic conversation history management.
- š¬ Session-based chat conversations
- š Automatic conversation history tracking
- š Secure API key handling
- š Easy to use and integrate
- š¦ Lightweight with minimal dependencies
``bash`
npm install @izumiapi/gemini-api
`javascript
import { ConnectToGemini } from '@izumiapi/gemini-api';
// Initialize with your Google AI API key
const gemini = new ConnectToGemini({
apiKey: 'your-google-ai-api-key'
});
// Start chatting with session management
const response = await gemini.chatSession({
idSesi: 'user123',
chat: 'Hello, how are you?'
});
console.log(response.result);
`
`javascript`
new ConnectToGemini({ apiKey })
Parameters:
- apiKey (string, required): Your Google AI API key
#### chatSession({ idSesi, chat })
Send a message and get a response while maintaining conversation history.
Parameters:
- idSesi (string, required): Unique session identifier for conversation trackingchat
- (string, required): The message to send to Gemini
Returns:
`javascript`
{
success: true,
result: "AI response text",
sessions: [...] // Complete conversation history
}
`javascript
const gemini = new ConnectToGemini({
apiKey: process.env.GOOGLE_AI_API_KEY
});
try {
const response = await gemini.chatSession({
idSesi: 'session-001',
chat: 'Explain quantum computing in simple terms'
});
console.log('AI Response:', response.result);
console.log('Conversation History:', response.sessions);
} catch (error) {
console.error('Error:', error.message);
}
`
`javascript
// Different users with separate conversation histories
const user1Response = await gemini.chatSession({
idSesi: 'user-1',
chat: 'What is JavaScript?'
});
const user2Response = await gemini.chatSession({
idSesi: 'user-2',
chat: 'How to cook pasta?'
});
// Continue user-1's conversation
const user1Followup = await gemini.chatSession({
idSesi: 'user-1',
chat: 'Can you give me an example?'
});
`
Create a .env file:
`env`
GOOGLE_AI_API_KEY=your_actual_api_key_here
`javascript
import dotenv from 'dotenv';
dotenv.config();
const gemini = new ConnectToGemini({
apiKey: process.env.GOOGLE_AI_API_KEY
});
`
1. Visit Google AI Studio
2. Sign in with your Google account
3. Create a new API key
4. Copy the key and use it in your application
> ā ļø Security Note: Never expose your API key in client-side code or commit it to version control. Always use environment variables.
The package includes built-in error handling for common scenarios:
`javascript`
try {
const response = await gemini.chatSession({
idSesi: 'test-session',
chat: 'Hello world!'
});
console.log(response.result);
} catch (error) {
if (error.message.includes('session ID')) {
console.error('Session ID is required');
} else if (error.message.includes('message')) {
console.error('Chat message is required');
} else {
console.error('Unexpected error:', error.message);
}
}
This package uses the gemini-2.0-flash-001` model, which provides:
- Fast response times
- High-quality text generation
- Multi-turn conversation support
- Currently in testing/maintenance phase
- Requires valid Google AI API key
- Internet connection required
- Subject to Google AI API rate limits and quotas
š§ Testing/Maintenance Phase
This package is actively being tested and maintained. Features may change, and we recommend:
- Testing thoroughly in development environments
- Checking for updates regularly
- Reporting issues on our repository
For questions, issues, or feature requests, please open an issue on our GitHub repository.
---
Note: This package is not officially affiliated with Google. Google AI and Gemini are trademarks of Google LLC.