Improve Gemini CLI API Key Authentication User Experience
What would you like to be added?
Currently, when a user attempts to authenticate the Gemini CLI using a Gemini API Key without the GEMINI_API_KEY environment variable set, they receive a generic error message. I propose enhancing this flow to provide clearer guidance, a direct link to generate an API key, and an interactive option to set the API key in a .env file directly from the CLI.
The proposed flow is as follows:
- Check for Existing API Key: When a user selects "Use Gemini API Key," the CLI will first check for the
GEMINI_API_KEYin both environment variables and a local.envfile. - Enhanced Error Message: If the key is not found, the error message should include a direct link to the API key generation page (e.g.,
https://aistudio.google.com/apikey). - Interactive API Key Input and
.envManagement: The CLI should then prompt the user to enter their API key. Upon receiving the input, it will either create a new.envfile or update an existing one in the current working directory, adding/updating theGEMINI_API_KEY="YOUR_API_KEY". This approach avoids potential permission issues associated with directly setting environment variables from the CLI. - Confirmation and Guidance: A success message should be displayed if the key is successfully saved to
.env. If saving fails, the user should be guided on how to manually set the API key (via.envorexport GEMINI_API_KEY="YOUR_API_KEY").
Why is this needed?
The current error message ("GEMINI_API_KEY environment variable not found...") is unhelpful and can be frustrating for new users. They are left without clear instructions on where to obtain an API key or how to properly set it up, which leads to a poor initial user experience.
By implementing an interactive flow that guides the user to generate an API key (with a direct link) and then helps them automatically configure it in a .env file, we can significantly reduce friction. This makes the onboarding process smoother, improves the discoverability of the API key generation page, and provides a more robust and user-friendly authentication setup for all users. Ultimately, this addresses a common pain point and enhances the overall usability of the Gemini CLI.
Additional context
Current Error Message Example:
GEMINI_API_KEY environment variable not found. Add that to your environment and try again (no reload needed if using .env)!
Proposed Enhanced Error Message Example:
GEMINI_API_KEY environment variable not found. You can generate an API Key at: https://aistudio.google.com/apikey
Proposed Interactive Prompt Example (after enhanced error):
Enter your Gemini API Key: [user enters key here]
Proposed Success Message Example:
API Key successfully saved to ./.env. Please restart your shell or source your .env file to apply changes.
This enhancement aims to provide a more intuitive and supportive experience for users setting up their Gemini API Key, which is crucial for a positive first impression and continued engagement with the CLI.
If this feature request is reviewed and approved, I'd be happy to personally work on the implementation and submit a Pull Request for it. I'm ready to contribute to improving the Gemini CLI's user experience.
Found possible duplicate issues:
- #2202: (#0.9312649050731933)
- #2290: (#0.9191891228911023)
If this feature request is reviewed and approved, I'd be happy to personally work on the implementation and submit a Pull Request for it. I'm ready to contribute to improving the Gemini CLI's user experience.
Since this feature has been added to a parent workstream in the public roadmap, I think it is safe to assume it is approved.
Do you still want to implement it?
May I know why this issue has been closed as completed without any merged PR..? @limehee
Found possible duplicate issues (updated):
- #11311
- #2202
- #11312
- #2290
- #5895
If you believe this is not a duplicate, please remove the status/possible-duplicate label.
@santhoshkumarCodes I apologize. I closed this issue by mistake, confusing it with another task. I have reopened the issue, so please check it when you can. Again, I'm sorry for the confusion.
Hello! As part of our effort to keep our backlog manageable and focus on the most active issues, we are tidying up older reports.
It looks like this issue hasn't been active for a while, so we are closing it for now. However, if you are still experiencing this bug on the latest stable build, please feel free to comment on this issue or create a new one with updated details.
Thank you for your contribution!