G4F – Complete Configuration Guide
This is the all-in-one reference for configuring GPT4Free:
Authentication, API Keys, Cookies, .har
files, Debug Mode, Proxy Setup, and GUI Authentication.
1. Environment Variable & API Key Configuration
Create a .env
file in your cookies/config directory:
HUUGINGFACE_API_KEY=
POLLINATIONS_API_KEY=
GEMINI_API_KEY=
TOGETHER_API_KEY=
DEEPINFRA_API_KEY=
OPENAI_API_KEY=
GROQ_API_KEY=
OPENROUTER_API_KEY=
2. API Key Setup
Set your API keys as environment variables:
# macOS / Linux
export OPENAI_API_KEY="your_key"
export GEMINI_API_KEY="your_key"
# Windows CMD
set OPENAI_API_KEY=your_key
# Windows PowerShell
$env:OPENAI_API_KEY = "your_key"
Example in Python:
import os
from g4f.client import Client
from g4f.Provider import OpenaiAPI
client = Client(provider=OpenaiAPI, api_key=os.getenv("OPENAI_API_KEY"))
4. .HAR
File Setup
What is a .HAR
File?
A .HAR
(HTTP Archive) file captures all network requests and cookies from a session.
Some providers like OpenaiChat
require a .HAR
file to authenticate without an API key.
Creating a .HAR
File:
- Login to the provider (e.g., chatgpt.com).
- Open Developer Tools (Right-click → Inspect or
F12
). - Go to the Network tab.
- Reload the page to capture activity.
- Perform at least one chat action/message.
- Right-click inside Network requests → Save all as HAR with content.
- Save it as
provider_name.har
inside./har_and_cookies/
.
🔒 Security Note: .HAR
files contain sensitive cookies — never share them.
Example Auto-Loading HAR in Python:
from g4f.cookies import read_cookie_files
read_cookie_files() # Auto-loads all .har and .json cookie files in the cookies dir
5. Debug Mode
Enable debug output:
import g4f.debug
g4f.debug.logging = True
Example debug log output:
Read .har file: ./har_and_cookies/you.com.har
Cookies added: 10 from .you.com
Read cookie file: ./har_and_cookies/google.json
Cookies added: 16 from .google.com
6. Proxy Configuration
Set a global proxy to hide/change your IP:
macOS / Linux:
export G4F_PROXY="http://host:port"
Windows CMD:
set G4F_PROXY=http://host:port
7. GUI Authentication
Set a password:
export G4F_API_KEY="mypassword"
Run the server:
python -m g4f --debug --port 8080 --g4f-api-key $G4F_API_KEY
Access web UI:
- Go to:
http://localhost:8080/chat/
- Username: anything (e.g.,
admin
) - Password: your G4F_API_KEY
8. Best Practices
- 🔒 Never hardcode credentials — use
.env
or environment variables - 🔄 Rotate keys every 90 days
- 🛡 Secure
.har
and.json
cookie files - 📊 Use rate limiting in production