Prerequisites: Node.js
- Install dependencies:
npm install - Set the
GEMINI_API_KEYin .env.local to your Gemini API key - Set the
GEMINI_API_KEYin a.envfile to your Gemini API key. You can copy the.env.exampleto a new file named.env. - Run the app:
npm run dev
- Go to Google AI Studio and log in with your Google Account.
- Click on "Create API Key".
- You may be prompted to create or select a Google Cloud project.
- Your API key will be generated. Copy and save it in a secure location.
You can deploy this application to either Vercel or GitHub Pages.
Vercel provides a seamless deployment experience for Vite apps.
-
Sign up and Install CLI:
- Create an account on Vercel.
- Install the Vercel CLI globally:
npm install -g vercel
-
Deploy:
- Log in to your Vercel account in the terminal:
vercel login - Run the deployment command from your project's root directory:
vercel - Vercel will guide you through the process, automatically detecting the Vite configuration.
- Log in to your Vercel account in the terminal:
-
Set Environment Variable:
- Go to your project's settings on the Vercel dashboard.
- Navigate to "Environment Variables".
- Add a new variable with the key
VITE_GEMINI_API_KEYand paste your Gemini API key as the value. For Vite apps, environment variables need to be prefixed withVITE_. - Redeploy your application for the changes to take effect.
-
Install
gh-pages:- Install the
gh-pagespackage as a dev dependency:npm install gh-pages --save-dev
- Install the
-
Configure
package.json:- Add a
homepagefield with the URL where your app will be hosted (e.g.,https://<YOUR-USERNAME>.github.io/<YOUR-REPO-NAME>/). - Add the following scripts:
"scripts": { "predeploy": "npm run build", "deploy": "gh-pages -d dist" }
- Add a
-
Configure
vite.config.ts:- In your
vite.config.tsfile, set thebaseproperty to your repository name (e.g.,/<YOUR-REPO-NAME>/). This ensures that all asset paths are correct.import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' export default defineConfig({ plugins: [react()], base: '/<YOUR-REPO-NAME>/' })
- In your
-
Deploy:
- Run the deploy script:
npm run deploy - This will build your application and push the
distfolder to agh-pagesbranch on your repository.
- Run the deploy script:
-
Set Environment Variable (using GitHub Actions):
- For public repositories, it's recommended to use a backend or a proxy to protect your API key. However, for simple deployments, you can use GitHub Actions and Secrets.
- Go to your GitHub repository's
Settings>Secrets and variables>Actions. - Create a new repository secret named
VITE_GEMINI_API_KEYand paste your Gemini API key. - Create a
.github/workflows/deploy.ymlfile with the following content:name: Build and Deploy on: push: branches: - main jobs: build-and-deploy: runs-on: ubuntu-latest steps: - name: Checkout 🛎️ uses: actions/checkout@v3 - name: Install and Build 🔧 run: | npm install npm run build env: VITE_GEMINI_API_KEY: ${{ secrets.VITE_GEMINI_API_KEY }} - name: Deploy 🚀 uses: peaceiris/actions-gh-pages@v3 with: github_token: ${{ secrets.GITHUB_TOKEN }} publish_dir: ./dist
- This workflow will automatically build and deploy your app whenever you push to the
mainbranch.
Important Note on API Keys: Remember that for client-side applications, your VITE_GEMINI_API_KEY will be visible in the browser's developer tools. For production applications, consider using a backend proxy to protect your API key.