An intelligent travel planning app that uses AI (Claude by Anthropic) to generate personalized travel itineraries based on your preferences!
Users enter their travel details:
- Origin and destination cities
- Travel dates
- Number of travelers
- Activity preferences (sightseeing, outdoor, museums, etc.)
- Food preferences (local cuisine, fine dining, street food, etc.)
The AI then generates a custom day-by-day itinerary tailored to these preferences!
.
├── index.html # Frontend form (runs in browser)
├── style.css # Styling
├── script.js # Frontend logic (collects data, displays results)
├── server.js # Backend server (calls AI API securely)
├── package.json # Node.js dependencies
├── .env # API keys (you'll create this)
└── .env.example # Template for .env file
- index.html: The form users fill out
- script.js: Collects form data and sends it to the backend
- style.css: Makes everything look nice
- server.js:
- Receives data from frontend
- Calls Claude AI API (with your API key)
- Sends the generated itinerary back to frontend
We can't call AI APIs directly from the browser because:
- Security: API keys would be visible to anyone (they could steal your key!)
- CORS: Browsers block many external API calls for security
- Control: Backend lets you monitor usage and add features
Before starting, you need:
- Node.js (version 14 or higher) - Download here
- Anthropic API Key - Get one here
Check if you have Node.js:
node --versionIf not installed, download from nodejs.org
In your project folder, run:
npm installThis installs:
- express: Web server framework
- @anthropic-ai/sdk: Claude AI client library
- dotenv: Loads environment variables from .env file
- Go to console.anthropic.com
- Sign up or log in
- Navigate to "API Keys"
- Create a new API key
- Copy the key (you'll only see it once!)
-
Copy the example environment file:
cp .env.example .env
-
Open
.envin a text editor:# On Mac/Linux nano .env # On Windows notepad .env
-
Replace
your_api_key_herewith your actual API key:ANTHROPIC_API_KEY=sk-ant-api03-xxxxxxxxxxxxx -
Save and close the file
.env file to GitHub! It's already in .gitignore.
Run:
npm startYou should see:
🚀 Server is running on http://localhost:3000
📝 Open your browser to see the app!
- Open your browser to
http://localhost:3000 - Fill out the travel form
- Click "Generate Itinerary!"
- Wait a few seconds while AI creates your custom itinerary
- View your personalized travel plan!
- Frontend (script.js) uses
fetch()to send HTTP POST requests - Backend (server.js) receives requests with
app.post() - Data is sent as JSON
// Frontend sends data
const response = await fetch('/generate-itinerary', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(userData)
});
// Backend receives data
app.post('/generate-itinerary', async (req, res) => {
const userData = req.body;
// Process the data...
});The backend calls Claude API like this:
const message = await anthropic.messages.create({
model: "claude-3-5-sonnet-20241022",
max_tokens: 4096,
messages: [{ role: "user", content: prompt }]
});Sensitive data (API keys) are stored in .env and loaded with dotenv:
require('dotenv').config();
const apiKey = process.env.ANTHROPIC_API_KEY;Modern JavaScript for handling asynchronous operations:
async function generateItinerary() {
const response = await fetch('/api'); // Wait for response
const data = await response.json(); // Wait for parsing
return data;
}Want to enhance the app? Try:
-
Add more preferences:
- Budget level (budget, moderate, luxury)
- Travel style (solo, family, romantic)
- Pace (relaxed, moderate, packed)
-
Better formatting:
- Parse the AI response more intelligently
- Add icons for activities
- Create a printable version
-
Save itineraries:
- Add a database (MongoDB, PostgreSQL)
- Let users save and revisit itineraries
- Share itineraries with friends
-
Use different AI models:
- Try OpenAI's GPT-4
- Compare results from different models
- Let users choose their preferred AI
Run: npm install
Make sure you created .env file with your API key
Change PORT = 3000 in server.js to another number (like 3001)
Double-check your API key in .env file - no spaces, no quotes
Check if Node.js is installed: node --version
Anthropic Claude API pricing (as of 2024):
- Claude 3.5 Sonnet: ~$3 per million input tokens, ~$15 per million output tokens
- Each itinerary costs roughly $0.01-0.05 depending on length
- You get $5 free credit when you sign up!
- ✅ Never commit
.envto GitHub (already in.gitignore) - ✅ Never share your API key publicly
- ✅ Rotate API keys if they're ever exposed
- ✅ Add rate limiting if deploying publicly
- ✅ Validate user input before sending to AI
Now that you have AI integration working, you can:
- Experiment with different prompts to get better itineraries
- Add more user inputs for even more personalization
- Learn about deploying to production (Heroku, Vercel, etc.)
- Build other AI-powered features!
Happy coding and safe travels! 🚀