How I integrated Google Gemini AI SDK and Google Calendar API to create CommunitySync—a conversational community management assistant
The Problem That Started It All
As someone deeply involved in community management, I've experienced firsthand the overwhelming burden of juggling multiple tools, spreadsheets, and platforms just to keep a community running smoothly. The constant context-switching between Google Calendar, Slack, spreadsheets, and various management tools wasn't just inefficient—it was exhausting.
The reality:
- Community managers spend 10+ hours per week on repetitive administrative tasks
- Critical events get missed because they're buried in spreadsheets
- Team coordination becomes a nightmare when information is scattered across platforms
- The focus shifts from building relationships to managing tools
I knew there had to be a better way—and Google Cloud Platform provided the perfect solution.
Introducing CommunitySync: Powered by GCP
CommunitySync is an AI-powered community management assistant built on Google Cloud Platform, leveraging the Google Gemini AI SDK and Google Calendar API to create a seamless, conversational experience.
Through natural conversation powered by Google's Gemini 2.5 Pro model, it helps community managers:
- Manage Events Seamlessly: Schedule, modify, and track community events using natural language—powered by Google Calendar API
- Calendar Integration: Full Google Calendar sync—view, create, and manage events directly from chat with real-time synchronization
- Proactive AI Assistance: Google Gemini AI anticipates needs and takes action, like automatically checking your calendar when you mention scheduling
- Context-Aware Conversations: Remembers your community's needs and preferences across conversations
Simply chat naturally: "Show me my upcoming events" or "Schedule a community meetup next Friday at 6 PM" and CommunitySync handles the rest—all powered by Google Cloud Platform.
The Architecture: GCP at the Core
CommunitySync is built on a modern architecture with Google Cloud Platform services at its heart:
┌─────────────────────────────────────────┐
│ Next.js 15 App Router │
│ (React Server Components + Actions) │
└─────────────────────────────────────────┘
│
┌───────────┴───────────┐
│ │
┌───────▼────────┐ ┌────────▼────────┐
│ Google Gemini │ │ Google Calendar│
│ AI SDK 2.5 │ │ API (GCP) │
└───────┬────────┘ └────────┬────────┘
│ │
│ Google Cloud OAuth │
│ Token Management │
└───────────┬───────────┘
│
┌───────────▼───────────┐
│ PostgreSQL (Neon) │
│ + Drizzle ORM │
└───────────────────────┘
Google Cloud Platform Services Used
- Google Gemini AI SDK: Powers the conversational AI assistant with advanced tool-calling capabilities
- Google Calendar API: Enables full calendar management through OAuth 2.0 integration
- Google Cloud Console: Manages OAuth credentials, API keys, and service configuration
- Google OAuth 2.0: Secure authentication and authorization flow
Tech Stack Highlights
- AI: Google Gemini 2.5 Pro via Vercel AI SDK (Google AI SDK integration)
- Calendar: Google Calendar API v3 with OAuth 2.0
- Framework: Next.js 15 with App Router and Turbo mode
- Database: Vercel Postgres (Neon) with Drizzle ORM
- UI: React 19, shadcn/ui, and Tailwind CSS
- Authentication: NextAuth.js with Google OAuth
Integrating Google Calendar API: The Deep Dive
The most complex and powerful part of CommunitySync is the Google Calendar API integration. Here's how it works:
Setting Up Google Cloud Platform
Before writing any code, you need to configure your GCP project:
-
Create a Google Cloud Project
- Navigate to Google Cloud Console
- Create a new project or select an existing one
-
Enable Google Calendar API
- Go to APIs & Services → Library
- Search for "Google Calendar API"
- Click "Enable"
-
Configure OAuth 2.0 Credentials
- Navigate to APIs & Services → Credentials
- Create OAuth client ID (Web application)
- Add authorized redirect URIs:
-
http://localhost:3000/api/auth/callback/google(development) -
https://yourdomain.com/api/auth/callback/google(production)
-
-
Set Up OAuth Consent Screen
- Configure scopes:
openid email profile https://www.googleapis.com/auth/calendar - Add test users (for development)
- Configure scopes:
-
Get Google Generative AI API Key
- Go to Google AI Studio
- Create an API key for Gemini
OAuth 2.0 Flow Implementation
The integration uses Google's OAuth 2.0 flow to securely access user calendars:
// OAuth 2.0 Client Setup
const oauth2Client = new google.auth.OAuth2(
process.env.GOOGLE_CLIENT_ID,
process.env.GOOGLE_CLIENT_SECRET,
process.env.GOOGLE_REDIRECT_URI
);
// Set credentials with refresh token
oauth2Client.setCredentials({
refresh_token: userRefreshToken
});
// Create Calendar API client
const calendar = google.calendar({
version: "v3",
auth: oauth2Client
});
Google Calendar API Tools
The AI assistant has access to five powerful calendar tools powered by the Google Calendar API:
1. List Events (listEvents)
Fetches calendar events with filtering and pagination:
async function listEvents({
calendarId = "primary",
timeMin,
timeMax,
maxResults = 10,
}: {
calendarId?: string;
timeMin?: string;
timeMax?: string;
maxResults?: number;
}) {
const calendar = await getCalendarClient();
const response = await calendar.events.list({
calendarId,
timeMin: timeMin ? new Date(timeMin).toISOString() : undefined,
timeMax: timeMax ? new Date(timeMax).toISOString() : undefined,
maxResults,
singleEvents: true,
orderBy: "startTime",
});
return {
events: response.data.items || [],
count: response.data.items?.length || 0,
};
}
2. Create Event (createEvent)
Creates new calendar events with full details:
async function createEvent({
calendarId = "primary",
summary,
description,
start,
end,
location,
attendees,
}: {
calendarId?: string;
summary: string;
description?: string;
start: string;
end: string;
location?: string;
attendees?: string[];
}) {
const calendar = await getCalendarClient();
const event = {
summary,
description,
location,
start: { dateTime: start, timeZone: "UTC" },
end: { dateTime: end, timeZone: "UTC" },
attendees: attendees?.map(email => ({ email })),
};
const response = await calendar.events.insert({
calendarId,
requestBody: event,
});
return response.data;
}
3. Update Event (updateEvent)
Updates existing calendar events:
async function updateEvent({
calendarId = "primary",
eventId,
summary,
description,
start,
end,
location,
}: {
calendarId?: string;
eventId: string;
summary?: string;
description?: string;
start?: string;
end?: string;
location?: string;
}) {
const calendar = await getCalendarClient();
// First, get the existing event
const existingEvent = await calendar.events.get({
calendarId,
eventId,
});
// Update with new values
const updatedEvent = {
...existingEvent.data,
summary: summary || existingEvent.data.summary,
description: description || existingEvent.data.description,
location: location || existingEvent.data.location,
start: start
? { dateTime: start, timeZone: "UTC" }
: existingEvent.data.start,
end: end
? { dateTime: end, timeZone: "UTC" }
: existingEvent.data.end,
};
const response = await calendar.events.update({
calendarId,
eventId,
requestBody: updatedEvent,
});
return response.data;
}
4. Delete Event (deleteEvent)
Removes events from the calendar:
async function deleteEvent({
calendarId = "primary",
eventId,
}: {
calendarId?: string;
eventId: string;
}) {
const calendar = await getCalendarClient();
await calendar.events.delete({
calendarId,
eventId,
});
return { success: true };
}
5. Check Availability (checkAvailability)
Checks for scheduling conflicts:
async function checkAvailability({
calendarId = "primary",
timeMin,
timeMax,
}: {
calendarId?: string;
timeMin: string;
timeMax: string;
}) {
const calendar = await getCalendarClient();
const response = await calendar.freebusy.query({
requestBody: {
timeMin: new Date(timeMin).toISOString(),
timeMax: new Date(timeMax).toISOString(),
items: [{ id: calendarId }],
},
});
const busy = response.data.calendars?.[calendarId]?.busy || [];
return {
available: busy.length === 0,
busyPeriods: busy,
};
}
Token Management with GCP
Secure token storage and refresh is critical for a production app:
// Store refresh token securely in database
export async function saveGoogleRefreshToken({
userId,
refreshToken,
}: {
userId: string;
refreshToken: string;
}) {
const db = getDb();
return await db
.update(user)
.set({
googleRefreshToken: refreshToken,
isCalendarConnected: true,
})
.where(eq(user.id, userId));
}
// Retrieve and use refresh token
async function getCalendarClient() {
const session = await auth();
const userToken = await getGoogleRefreshToken({
userId: session.user.id
});
const oauth2Client = new google.auth.OAuth2(
process.env.GOOGLE_CLIENT_ID,
process.env.GOOGLE_CLIENT_SECRET,
process.env.GOOGLE_REDIRECT_URI
);
oauth2Client.setCredentials({ refresh_token: userToken });
// Automatically refreshes access token when needed
return google.calendar({ version: "v3", auth: oauth2Client });
}
Google Gemini AI SDK Integration
The Google Gemini AI SDK powers the conversational intelligence of CommunitySync. Here's how it's integrated:
Setting Up Google Gemini
// ai/index.ts
import { google } from "@ai-sdk/google";
export const geminiProModel = google("gemini-2.0-flash-exp");
// Or use Gemini Pro
export const geminiProModel = google("gemini-1.5-pro");
Connecting Gemini to Calendar Tools
The AI SDK's streamText API connects Gemini to the Google Calendar API tools:
import { streamText } from "ai";
import { geminiProModel } from "@/ai";
import { googleCalendarTools } from "@/lib/tools/google-calendar";
const result = streamText({
model: geminiProModel,
system: `You are an AI Community Management Assistant.
You have direct access to the user's Google Calendar through the available tools.
When users ask about their calendar, IMMEDIATELY use the listEvents tool.
Use "primary" as the default calendarId unless specified otherwise.`,
messages: modelMessages,
tools: {
...googleCalendarTools, // All 5 Calendar API tools
getWeather: { /* ... */ },
},
});
Tool Calling with Gemini
Gemini excels at tool calling—it automatically selects the right Google Calendar API tool based on user intent:
- User: "Show me my calendar" → Calls
listEvents - User: "Schedule a meeting tomorrow" → Calls
createEvent - User: "Am I free Friday?" → Calls
checkAvailability
The AI SDK handles the tool execution and returns results seamlessly.
Key Features Powered by GCP
1. Natural Language Calendar Management
Powered by Google Gemini AI SDK and Google Calendar API, users can manage their entire calendar through conversation:
- View events: "Show me my calendar for next week"
- Create events: "Schedule a team standup tomorrow at 10 AM"
- Update events: "Move the meeting to 2 PM"
- Check availability: "Am I free next Friday afternoon?"
2. Real-Time Synchronization
Events created through the AI assistant are immediately synchronized with Google Calendar via the Calendar API. Changes are reflected in real-time across all devices.
3. Secure OAuth Integration
Google OAuth 2.0 ensures secure access to user calendars. Refresh tokens are stored securely, and access tokens are automatically refreshed when needed.
4. Proactive AI Assistance
Google Gemini AI anticipates needs and takes action automatically. When you mention scheduling, it checks your calendar for conflicts using the Calendar API.
Challenges and Solutions with GCP Integration
Challenge 1: OAuth Token Management
Problem: Managing Google OAuth refresh tokens securely while ensuring they don't expire unexpectedly.
Solution:
- Secure token storage in PostgreSQL database
- Automatic token refresh using Google's OAuth 2.0 client library
- Connection status tracking in the database
- User-friendly re-authentication flow
Challenge 2: Google Calendar API Rate Limits
Problem: Google Calendar API has rate limits that could cause failures.
Solution:
- Implemented proper error handling for rate limit responses
- Added retry logic with exponential backoff
- Cached frequently accessed calendar data
- User-friendly error messages when limits are reached
Challenge 3: AI Tool Selection with Gemini
Problem: Getting Gemini to automatically use the right Calendar API tool without asking unnecessary questions.
Solution:
- Refined system prompts with explicit instructions about Google Calendar API
- Updated tool descriptions to guide Gemini's behavior
- Added default values (like "primary" calendarId) to tool schemas
- Implemented proactive tool calling patterns
Key Learning: Google Gemini's tool-calling capabilities are excellent, but prompt engineering is critical for optimal behavior.
Challenge 4: Real-Time Calendar Synchronization
Problem: Calendar events created through the AI weren't immediately visible in the calendar panel.
Solution:
- Implemented SWR for data fetching with automatic revalidation
- Added real-time cache invalidation after Calendar API operations
- Created optimistic updates for instant feedback
- Built refresh mechanisms using Google Calendar API webhooks (future enhancement)
GCP Best Practices Implemented
1. Secure Credential Management
- OAuth credentials stored as environment variables
- Refresh tokens encrypted in the database
- Never expose API keys in client-side code
- Use Google Cloud Secret Manager for production (recommended)
2. Efficient API Usage
- Batch requests when possible
- Cache calendar data appropriately
- Handle rate limits gracefully
- Use appropriate scopes (only request what you need)
3. Error Handling
- Comprehensive error handling for all Google Calendar API calls
- User-friendly error messages
- Graceful degradation when APIs are unavailable
- Proper logging for debugging
4. Token Refresh Strategy
- Automatic token refresh using Google's OAuth client
- Store refresh tokens securely
- Handle token expiration gracefully
- Provide clear feedback when re-authentication is needed
What Makes This GCP Integration Special
1. Seamless Google Services Integration
Combining Google Gemini AI SDK and Google Calendar API creates a powerful, integrated experience. The AI understands calendar context and can take actions automatically.
2. Production-Ready OAuth Implementation
Secure OAuth 2.0 flow with proper token management ensures users' data is protected while providing a smooth experience.
3. Type-Safe API Integration
Using TypeScript with Zod schemas ensures type safety for all Google Calendar API interactions, catching errors at compile-time.
4. Scalable Architecture
The architecture is designed to scale with Google Cloud Platform services, handling multiple users and high API request volumes.
Lessons Learned from GCP Integration
1. Google Calendar API is Powerful but Complex
The Calendar API provides extensive functionality, but proper setup and understanding of OAuth flows is essential. Take time to understand the authentication model.
2. Gemini AI SDK Tool Calling is Excellent
Google Gemini excels at tool calling when properly configured. Well-crafted prompts and tool descriptions dramatically improve AI behavior.
3. OAuth Token Management is Critical
Proper token storage, refresh, and error handling are essential for a production app. Google's OAuth client libraries handle much of this, but you still need to manage refresh tokens securely.
4. Rate Limits Matter
Google Calendar API has rate limits. Implement proper error handling, caching, and retry logic to handle these gracefully.
5. User Experience Over Features
A few well-executed Google Calendar API features beat many incomplete ones. Focus on making the core calendar management seamless and reliable.
Getting Started with GCP Integration
Want to build something similar? Here's how to get started:
1. Set Up Google Cloud Project
# Enable Google Calendar API
gcloud services enable calendar-json.googleapis.com
# Or use the Google Cloud Console
# APIs & Services → Library → Google Calendar API → Enable
2. Configure OAuth Credentials
- Go to Google Cloud Console
- APIs & Services → Credentials
- Create OAuth client ID (Web application)
- Add authorized redirect URIs
3. Install Dependencies
pnpm add googleapis @ai-sdk/google ai
4. Set Up Environment Variables
GOOGLE_CLIENT_ID=your_client_id
GOOGLE_CLIENT_SECRET=your_client_secret
GOOGLE_REDIRECT_URI=http://localhost:3000/api/auth/callback/google
GOOGLE_GENERATIVE_AI_API_KEY=your_gemini_api_key
5. Initialize Google APIs
import { google } from "googleapis";
const oauth2Client = new google.auth.OAuth2(
process.env.GOOGLE_CLIENT_ID,
process.env.GOOGLE_CLIENT_SECRET,
process.env.GOOGLE_REDIRECT_URI
);
const calendar = google.calendar({ version: "v3", auth: oauth2Client });
What's Next: Expanding GCP Integration
Future enhancements leveraging Google Cloud Platform:
Google Workspace Integration: Gmail, Google Drive, and Google Meet integration
Google Cloud Functions: Serverless functions for background calendar processing
Google Cloud Pub/Sub: Real-time calendar event notifications
Google Cloud Storage: Enhanced file storage for community assets
Google Analytics Integration: Track community engagement metrics
Multi-Calendar Support: Manage multiple Google Calendars simultaneously
Conclusion
Building CommunitySync with Google Cloud Platform, Google Gemini AI SDK, and Google Calendar API has been an incredible learning experience. The integration of these powerful Google services creates a seamless, intelligent assistant that transforms how communities are managed.
The combination of Google's AI capabilities with their Calendar API provides a foundation for building powerful, conversational applications that understand context and take action automatically.
The future of community management is conversational, intelligent, and powered by Google Cloud Platform.
Built with Google Cloud Platform, Google Gemini AI SDK, Google Calendar API, Next.js 15, and React 19.
Want to learn more?
Top comments (0)