How to Configure API Keys for OpenAI and Anthropic Models in TextFlow
TextFlow reads API credentials from the config.json file, where you replace placeholder values for OPENAI_API_KEY and ANTHROPIC_API_KEY with your actual secrets, and the src/config.py loader automatically makes them available to the model clients at runtime.
TextFlow is an open-source project by junyiye/textflow that simplifies working with large language models. To use OpenAI or Anthropic models within the framework, you must configure your API keys in the project's configuration file. This guide explains exactly how TextFlow handles authentication and where to place your credentials.
Where TextFlow Stores API Credentials
TextFlow centralizes all API authentication in a single JSON configuration file. The system uses two key files to manage credentials:
config.json– Located in the project root, this file contains theapi_keysdictionary with placeholder values for all supported providers.src/config.py– This loader script readsconfig.jsonat import time and exposes the configuration as a Python dictionary.
When the application starts, src/config.py parses config.json and makes the entire configuration object available to other modules. This design ensures that API keys are loaded once and reused throughout the application lifecycle.
How to Update Your API Keys in config.json
To authenticate with OpenAI or Anthropic, you must replace the placeholder strings in config.json with your actual API secrets. The configuration file expects specific key names within the api_keys object:
- OpenAI: Set
"OPENAI_API_KEY"to your OpenAI secret key (typically starting withsk-). - Anthropic: Set
"ANTHROPIC_API_KEY"to your Anthropic secret key (typically starting withsk-ant-).
After updating these values, save the file. TextFlow will automatically use these credentials the next time you run your application. For security, consider adding config.json to your .gitignore file to prevent accidental commits of sensitive keys to version control.
How the API Keys Are Loaded at Runtime
When you request a model in TextFlow, the system dynamically creates the appropriate client object using your configured credentials. This process occurs in src/models/api_models.py, which contains the load_api_model() function.
The function accesses config["api_keys"] from the globally loaded configuration and passes the appropriate key to the provider's SDK:
- For OpenAI models, it instantiates
OpenAI(api_key=config["api_keys"]["OPENAI_API_KEY"]). - For Anthropic models, it instantiates
Anthropic(api_key=config["api_keys"]["ANTHROPIC_API_KEY"]).
This architecture ensures that API keys are never hardcoded in model logic and can be updated centrally without modifying source code.
Complete Configuration Example
Here is a practical example showing how to programmatically update your config.json file with valid API keys:
import json
import pathlib
# Path to the configuration file
config_path = pathlib.Path("config.json")
# Load existing configuration
config = json.loads(config_path.read_text())
# Insert your actual API keys
config["api_keys"]["OPENAI_API_KEY"] = "sk-XXXXXXXXXXXXXXXXXXXXXXXX"
config["api_keys"]["ANTHROPIC_API_KEY"] = "claude-XXXXXXXXXXXXXXXXXXXXXXXX"
# Save updated configuration
config_path.write_text(json.dumps(config, indent=2))
print("API keys updated successfully.")
This script preserves the existing structure of your configuration while securely injecting your credentials.
Loading Models After Configuration
Once your API keys are configured, you can immediately start using OpenAI and Anthropic models through TextFlow's unified interface. Import the load_api_model function from the API models module to instantiate clients:
from models.api_models import load_api_model
# Initialize OpenAI client (GPT-4o)
openai_client = load_api_model("gpt-4o")
# Initialize Anthropic client (Claude 3.5 Sonnet)
anthropic_client = load_api_model("claude-3-5-sonnet")
The function automatically retrieves the appropriate API key from the configuration dictionary created when src/config.py loaded config.json at startup.
Summary
Configuring API keys for OpenAI and Anthropic in TextFlow requires editing a single JSON file:
- Configuration location:
config.jsonin the project root contains theapi_keysdictionary. - Key names: Use
OPENAI_API_KEYfor OpenAI andANTHROPIC_API_KEYfor Anthropic. - Runtime loading:
src/config.pyloads the file at import time, andsrc/models/api_models.pypasses keys to the respective SDK clients. - Security: Add
config.jsonto.gitignoreto prevent exposing secrets in version control.
Frequently Asked Questions
Where does TextFlow store API configuration?
TextFlow stores all API credentials in the config.json file located in the repository root. The src/config.py module loads this file at runtime and exposes the configuration as a Python dictionary to the rest of the application.
What are the exact key names required for OpenAI and Anthropic?
For OpenAI models, set the OPENAI_API_KEY field in the api_keys dictionary. For Anthropic models, set the ANTHROPIC_API_KEY field. These exact strings are required because src/models/api_models.py looks up these specific keys when instantiating the provider clients.
How do I prevent my API keys from being committed to Git?
Add config.json to your .gitignore file before entering real credentials. Alternatively, you can use environment variable substitution in your application code, though the standard TextFlow implementation expects direct values in the JSON configuration file.
Can I configure multiple API keys for the same provider?
The current implementation in src/models/api_models.py uses a single key per provider from the config["api_keys"] dictionary. If you need to rotate between multiple keys or use different accounts, you would need to modify the configuration structure and update the loader logic to support arrays or nested provider objects.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →