These are the code samples that are found in the 3rd post in the series introducing the Gemini API.
❗ API key required |
---|
An API key is required to use Google's Gemini API. Follow the instructions below. The app will not run without a .env file (Node.js) or settings.py (Python). |
App | Description | Platform |
---|---|---|
webgem/nodejs/main.js |
Express.js sample app | Node.js (CommonJS) |
webgem/nodejs/main.mjs |
Express.js sample app | Node.js (modern JS/ECMAScript module) |
webgem/nodejs/templates/index.html |
Web template | Nunjucks (identical to Jinja2) |
webgem/python/main.py |
Flask sample app | Python 3 |
webgem/python/templates/index.html |
Web template | Jinja2 (identical to Nunjucks) |
- Ensure your Node (and NPM) installation is up-to-date (recommend 18+)
cd nodejs
- Install packages:
npm i dotenv express multer nunjucks sharp @google/generative-ai
- Create API key and set in
.env
file asAPI_KEY = <YOUR_API_KEY>;
- Run
node main.js
ornode main.mjs
- Ensure your Python (and
pip
) installation is up-to-date (recommend 3.9+) cd python
and optionally create & activate a virtual environment ("virtualenv") for isolation- Install packages:
pip install -U pip flask pillow google-generativeai
(orpip3
) - Create API key and set in
settings.py
file asAPI_KEY = <YOUR_API_KEY>
... you can alternatively put it into.env
and usepython-dotenv
, similar to the Node version. - Run
python main.py
(orpython3
)
📝 Gemini API also accessible from GCP Vertex AI |
---|
While these sample apps access the Gemini API from Google AI, a great place for beginners and those experimenting with it, the API is also accessible from GCP's Vertex AI platform for those ready to use Gemini for production workloads. To learn more about calling the Gemini API from either (or both) places, see my "A better Gemini API 'Hello World!' sample" post, the first in my multi-part series on the Gemini API. |