← All posts

Secure API Key Management for AI Tools

Learn the most practical and secure ways to manage API keys for AI tools and MCP servers across different shells like Bash, Zsh, and Fish.

Close-up of colorful syntax-highlighted code on a dark screen

When setting up AI tools like MCP servers, you often need to store sensitive API keys as environment variables. Here is the most practical way to do it for Bash/Zsh and Fish shell.

1. For Zsh or Bash (.zshrc / .bashrc)

Most macOS users (on Zsh) or Linux users (on Bash) store variables in a hidden configuration file. These files are read every time a new terminal window opens.

  1. Open your config file:

    • Zsh: nano ~/.zshrc
    • Bash: nano ~/.bash_profile (or ~/.bashrc)
  2. Add your key at the bottom:

    bash
    export GITHUB_MCP_PAT="ghp_your_token_here"
    export CONTEXT7_API_KEY="c7_your_key_here"
  3. Save and Exit: Press Ctrl+O, Enter, then Ctrl+X.

  4. Activate changes: Run source ~/.zshrc (or restart your terminal).

2. For Fish Shell (Universal Variables)

Fish shell handles things differently. Instead of editing a text file, you can set a Universal Variable directly in the command line. This is cleaner and more secure.

  1. Run the command once:

    fish
    set -Ux GITHUB_MCP_PAT ghp_your_token_here
    set -Ux CONTEXT7_API_KEY c7_your_key_here
  2. Why this is better:

    • Persistence: It survives restarts without needing a “source” command.
    • No File Bloat: It is stored in Fish’s internal database, not in your config.fish text file.
    • Security: If you share your config.fish online, your api keys stay hidden.

Summary Table

Feature Zsh / Bash Fish (Universal)
Storage Plain text file (.zshrc) Internal binary database
Command export KEY="value" set -Ux KEY value
Requires Restart? Yes (or source) No (immediate)
Best for Dotfiles Manual management Automatic isolation

Never commit your .zshrc or config.fish to a public GitHub repository if you have written your API keys directly into them.

Written by Mac Poncardas, a freelance web designer and developer helping small businesses get more clients from their websites.

More writing

Does your site pass the test?

Send me the link. I'll tell you what I'd change first, free.