Is My Grok (xAI) API Key Safe in My App?
Short answer: not if it's in your front-end code. xAI's docs say to never expose your API key in client-side code. If your app calls Grok straight from the page, anyone can pull the key out and use it on your bill. Move the calls to a server and keep the key there.
Check your live app for free
Paste your link. The free summary usually takes under a minute.
See an example report (for example.org) · a real free scan of a test site, so you can see what you get
Why this comes up
You ask Lovable, Bolt, Cursor or a similar tool to "add a Grok chat" or "use Grok to write replies." A quick way to make that work is to call the xAI API right from the page, with the key pasted in. It works on day one. But the page's code is sent to every visitor's browser, and the key goes with it.
What xAI says
xAI's docs now use the company's newer name, SpaceXSI, and call the dashboard the Grok Console. The pages quoted here are on that site.
The API security FAQ says: "Treat your Grok API keys as sensitive information, like passwords or credit card details." It also says: "Store keys securely using environment variables or secret management tools. Avoid committing keys to public repositories or source code."
The page about short-lived tokens for voice apps is even more direct: "Never expose your API key in client-side code. Always use ephemeral tokens for browser and mobile applications." That page is about connecting to xAI's Speech to Speech API from a browser or phone. For a normal chat or text call, the safe way is the same idea: your server holds the key.
"Client-side code" is the code that runs in your visitor's browser. A "back end" is your own server code (for example a Supabase Edge Function or a server API route). It holds the key and calls Grok for your app. The browser talks to your server, never to xAI with your key.
"But my env variable is hidden, right?"
Not always. Putting the key in an environment variable keeps it out of your code files. But in front-end tools like Vite or Next.js, some variables are copied into the code that goes to the browser.
Vite's docs say: "Variables prefixed with VITE_ will be exposed in client-side source code after Vite bundling." They also say: "VITE_* variables should not contain sensitive information such as API keys." (Vite: Env Variables and Modes)
Next.js says: "these public environment variables will be inlined into the JavaScript bundle during next build." That is about variables that start with NEXT_PUBLIC_. (Next.js: How to use environment variables)
If the page calls Grok directly, the key has to be in the browser for that to work, so it's public no matter where you stored it.
How to check your app
- Ask your AI builder. Paste this:
Find every place my app uses an xAI or Grok API key. For each one, tell me: is the key used in code that runs in the browser, or only in server code (an Edge Function or server API route)? Check environment variables that start with VITE_ or NEXT_PUBLIC_ too. Don't change any code yet.- Run the free scan. Paste your URL in the box at the top. It looks for keys that look like xAI keys in the HTML tags on the page (including tag attributes), the inline scripts on that page, and up to 15 script files from your own site. More on what it can and can't see is below.
How to fix it
If the key is in your front-end code:
- Move the Grok calls to a server. Paste this:
Move all xAI (Grok) API calls out of my front-end code into a server function (a Supabase Edge Function or a server API route). Store the xAI key as a server-only secret, not in any VITE_ or NEXT_PUBLIC_ variable. Have my app call my server function instead, and only let logged-in users call it. Then show me that no xAI key is left in any file that runs in the browser.- Treat the old key as leaked. Make a new key, update your server secret, check the app works, then turn off the old one. The xAI FAQ says that if you suspect a leak, log into the Grok Console first and make sure you are viewing the correct team, "as API keys are tied to specific teams." Then open the "API Keys" section. The FAQ says: "Select 'Disable key' to deactivate it temporarily or 'Delete key' to remove it permanently. Then, click the 'Create API Key' button to generate a new one and update your applications." (xAI Docs: FAQ, API security)
- Watch your usage and set limits. The FAQ says: "Monitor your account for unusual activity to stay protected." See also How do I stop a surprise bill on my vibe-coded app?
Things to watch for
- Don't put the key in Git. The FAQ says the company "partners with GitHub's Secret Scanning program to detect leaked keys. If a leak is found, we disable the key and notify you via email." That helps, but a disabled key also breaks your app until you add a new one. Keep
.envfiles out of Git. See Is your .env file public? - Don't share one key. The FAQ says: "Do not share keys between teammates to avoid unauthorized access."
- Your server function needs limits too. Once Grok runs through your server, anyone who can reach that function can still run up costs. Require a login and limit how often each user can call it. See Limit how often people can try to log in.
What the rooGBotVC scan checks
Paste your URL in the box at the top. The scan reads the HTML tags on the page you scan (including tag attributes), the inline scripts on that page, and up to 15 script files from your own site.
- If it finds a key that looks like an xAI key, it shows "xAI API key exposed in public code" (critical). It looks for keys that start with
xai-followed by a long string of letters and numbers. Found keys are shown with most of the key hidden.
What the scan can't do:
- It doesn't run your JavaScript, so a key that only shows up after the page runs, or that is loaded from another site, may be missed.
- If your xAI key looks different from that pattern, the scan may not catch it.
- It can't see your Grok Console, so it can't tell whether a key is disabled, how much it has been used, or what limits you have set.
- It never uses or tests any key it finds.
- It doesn't test logins, rate limits, billing or RLS.
Related reading
- Exposed API keys: what to do when any key leaks.
- Is my OpenAI API key safe in my app?: the same idea for an OpenAI key.
- Is my Claude API key safe in my app?: the same idea for an Anthropic Claude key.
- Is my Gemini API key safe in my app?: the same idea for Google's Gemini key.
- Are your source maps public?: another way your code can show more than you think.
The bottom line
A Grok key in front-end code is a public key, and xAI says never to expose it in client-side code. Move Grok calls to your own server, make a new key, turn off the old one, and keep an eye on your usage. Then paste your URL in the box at the top to check what your app shows the public.
Related guides
This is an automated, surface-level check of what your site shows the public. It is not a full security audit.