CGI Mode
CGI overview
CGI (Common Gateway Interface) allows web servers to execute scripts and return their output as HTTP responses.
CGI mode is primarily supported by the Python and Perl SDKs. Compiled languages (Go, C++, Java) are better served by their built-in HTTP servers. TypeScript and Ruby can use CGI via their respective interpreters but this is uncommon.
| TypeScript | Possible | Via node as CGI; prefer server mode instead |
Benefits:
- Works with shared hosting
- Simple deployment - just upload files
- No separate process management
- Compatible with Apache, nginx
Drawbacks:
- New process per request (slower)
- No persistent connections
- Limited scalability
CGI detection
The SDK detects CGI mode via the GATEWAY_INTERFACE environment variable:
Basic CGI script
Python
Make scripts executable:
CGI request flow

Apache configuration
Enable CGI
Virtual host configuration
nginx configuration
nginx doesn’t natively support CGI, but you can use FastCGI with fcgiwrap:
CGI host configuration
In CGI mode, the SDK needs to know the external hostname for generating URLs:
Or set environment variable:
Testing CGI locally
Use swaig-test to simulate CGI environment:
Authentication in CGI mode
The SDK checks basic auth in CGI mode:
If authentication fails, returns 401 with WWW-Authenticate header.
Directory structure
Shared hosting deployment
For shared hosting where you can’t install system packages:
Install packages locally:
CGI best practices
Performance
- Keep imports minimal - each request starts fresh
- Consider FastCGI for better performance
- Cache what you can (but remember process dies)
Security
- Set proper file permissions (750 or 755)
- Don’t expose .py files directly if possible
- Use HTTPS always
- Set auth credentials as environment variables
Debugging
- Check web server error logs
- Verify shebang line (#!/usr/bin/env python3)
- Test script from command line first
- Ensure proper line endings (LF, not CRLF)
Common CGI issues
Migration from CGI
CGI to FastCGI
Keep same code, use fcgiwrap or gunicorn. Better performance, persistent processes.
CGI to server mode
Same code works - just run differently (python agent.py instead of CGI). Add systemd service, nginx reverse proxy.
CGI to serverless
Same code works with minor changes. Add Lambda handler wrapper. Deploy to AWS/GCP/Azure.