A TypeScript application that generates beautiful storybooks using Google Gemini AI and automatically saves them as PDFs to Google Drive.
- AI-Powered Story Generation: Uses Google Gemini AI to create engaging stories
- PDF Generation: Converts stories into beautifully formatted PDF storybooks
- Local Storage: Saves PDFs locally in the
generated-pdfs/directory before uploading - Google Drive Integration: Automatically uploads PDFs to your specified Google Drive folder
- Configurable Parameters: Customize story length, target audience, and more
- RESTful API: Simple POST endpoint for easy integration
- Vercel Ready: Optimized for deployment on Vercel
- TypeScript - Type-safe development
- Next.js - React framework with API routes
- Google Gemini AI - Story content generation
- Puppeteer - PDF generation
- Google Drive API - Cloud storage
- Vercel - Deployment platform
Before setting up the application, you'll need:
-
Google Gemini API Key
- Visit Google AI Studio
- Create a new API key
-
Google Cloud Service Account
- Go to Google Cloud Console
- Create a new project or select an existing one
- Enable the Google Drive API
- Create a service account and download the JSON key file
-
Google Drive Folder
- Create a folder in Google Drive where PDFs will be stored
- Share the folder with your service account email
- Note the folder ID from the URL
-
Clone the repository
git clone <repository-url> cd story-generator
-
Install dependencies
npm install
-
Set up environment variables
cp .env.example .env.local
Edit
.env.localwith your configuration:GEMINI_API_KEY=your_gemini_api_key_here GOOGLE_DRIVE_FOLDER_ID=your_google_drive_folder_id_here GOOGLE_SERVICE_ACCOUNT_KEY={"type":"service_account",...}
-
Run the development server
npm run dev
-
Open your browser Navigate to
http://localhost:3000
-
Install Vercel CLI
npm i -g vercel
-
Deploy
vercel
-
Set environment variables in Vercel
vercel env add GEMINI_API_KEY vercel env add GOOGLE_DRIVE_FOLDER_ID vercel env add GOOGLE_SERVICE_ACCOUNT_KEY
POST /api/generate-storybook
{
"description": "A story about a brave little mouse",
"title": "The Adventures of Squeaky",
"targetAudience": "children",
"length": "medium"
}description(required): Brief description of the story you want to generatetitle(optional): Custom title for the storybooktargetAudience(optional):"children","young-adult", or"adult"(default:"children")length(optional):"short","medium", or"long"(default:"medium")
{
"success": true,
"message": "Storybook generated and uploaded successfully",
"fileId": "1ABC123DEF456GHI789JKL",
"fileName": "the-adventures-of-squeaky-2024-01-15.pdf",
"driveUrl": "https://drive.google.com/file/d/1ABC123DEF456GHI789JKL/view",
"localPath": "/path/to/project/generated-pdfs/the-adventures-of-squeaky-2024-01-15.pdf"
}Note: PDFs are saved locally in the generated-pdfs/ directory before being uploaded to Google Drive. This directory is automatically created if it doesn't exist and is excluded from version control.
curl -X POST https://your-app.vercel.app/api/generate-storybook \
-H "Content-Type: application/json" \
-d '{
"description": "A magical adventure about a young wizard learning to fly",
"targetAudience": "children",
"length": "medium"
}'const response = await fetch('/api/generate-storybook', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
description: 'A magical adventure about a young wizard learning to fly',
targetAudience: 'children',
length: 'medium'
})
});
const result = await response.json();
console.log(result.driveUrl); // Google Drive link to the PDF- Keep your API keys secure and never commit them to version control
- The Google service account should have minimal permissions (only Google Drive access)
- Consider implementing rate limiting for production use
- Validate and sanitize all user inputs
-
"Gemini API key not configured"
- Ensure
GEMINI_API_KEYis set in your environment variables - Verify the API key is valid and has proper permissions
- Ensure
-
"Google Drive configuration missing"
- Check that
GOOGLE_DRIVE_FOLDER_IDandGOOGLE_SERVICE_ACCOUNT_KEYare set - Ensure the service account has access to the specified folder
- Check that
-
"Failed to generate PDF"
- This might be a Puppeteer issue on Vercel
- Ensure your deployment has sufficient memory allocated
-
"Failed to upload to Google Drive"
- Verify the service account has write permissions to the folder
- Check that the Google Drive API is enabled in your Google Cloud project
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
If you encounter any issues or have questions, please open an issue on GitHub.