API Reference
This page contains the complete API documentation for Hostify.
Host Class
- class hostify.Host(domain, port=None, path=None, api_token=None)[source]
Bases:
objectMain class for hosting applications via Cloudflare Tunnels.
- Simple one-line API:
Host(domain=”app.example.com”, port=3000).serve()
- Or with static files:
Host(domain=”app.example.com”, path=”./public”).serve()
- __init__(domain, port=None, path=None, api_token=None)[source]
Initialize Host instance.
- Parameters:
domain (
str) – Full domain or subdomain (e.g., “app.example.com”)port (
Optional[int]) – Port of existing local server (mutually exclusive with path)path (
Optional[str]) – Path to static files to serve (mutually exclusive with port)api_token (
Optional[str]) – Cloudflare API token (optional, reads from CF_API_TOKEN env var)
- Raises:
HostError – If configuration is invalid
The Host class is the main entry point for using Hostify. It manages the entire lifecycle of hosting your application through Cloudflare Tunnels.
Constructor Parameters
- class Host(domain, port=None, path=None, api_token=None)
Initialize a Host instance.
- Parameters:
domain (str) – Full domain or subdomain (e.g., “app.example.com”). Required.
port (int) – Port number where your application is running (1-65535). Mutually exclusive with
path.path (str) – Path to directory containing static files to serve. Mutually exclusive with
port.api_token (str) – Cloudflare API token. If not provided, reads from
CF_API_TOKENenvironment variable.
- Raises:
HostError – If configuration is invalid (e.g., both port and path specified, or neither specified).
Note
You must specify either
portORpath, but not both.
Methods
- serve()
Start hosting the application.
This method:
Validates the local server or starts static server
Creates Cloudflare tunnel
Creates DNS record
Starts cloudflared process
Monitors and keeps the connection alive
- Raises:
HostError – If setup fails at any step.
- Returns:
None (blocks until Ctrl+C is pressed)
Example:
from hostify import Host host = Host(domain="app.example.com", port=3000) host.serve() # Blocks until Ctrl+C
- cleanup()
Clean up all resources.
This method is called automatically when you press Ctrl+C or when the program exits. It:
Stops cloudflared process
Deletes DNS record
Deletes tunnel
Stops static server if running
You typically don’t need to call this manually.
- Returns:
None
Exceptions
Cloudflare Module
- class hostify.cloudflare.Cloudflare(api_token=None)[source]
Bases:
objectCloudflare API client for managing tunnels and DNS records.
Requires CF_API_TOKEN or CLOUDFLARE_API_TOKEN environment variable with permissions: - Account → Cloudflare Tunnel → Edit - Zone → DNS → Edit - Zone → Read
- BASE_URL = 'https://api.cloudflare.com/client/v4'
- __init__(api_token=None)[source]
Initialize Cloudflare API client.
- Parameters:
api_token (
Optional[str]) – Cloudflare API token. If None, reads from CF_API_TOKEN or CLOUDFLARE_API_TOKEN env var.- Raises:
CloudflareAPIError – If API token is not provided.
- configure_tunnel_route(tunnel_id, hostname, service)[source]
Configure a route for a tunnel (public hostname).
- Parameters:
tunnel_id (
str) – Tunnel IDhostname (
str) – Hostname to route (e.g., “app.example.com”)service (
str) – Service URL (e.g., “http://localhost:8000”)
- Return type:
- Returns:
Route configuration dict
- create_dns_record(zone_id, subdomain, tunnel_id)[source]
Create a CNAME DNS record pointing to a tunnel.
- get_account_id()[source]
Get the first available account ID.
- Return type:
- Returns:
Account ID string.
- Raises:
CloudflareAPIError – If no accounts found.
- get_zone_id(domain)[source]
Get zone ID for a domain.
- Parameters:
domain (
str) – Domain name (e.g., “example.com”)- Return type:
- Returns:
Zone ID string.
- Raises:
CloudflareAPIError – If zone not found.
Cloudflared Module
- class hostify.cloudflared.Cloudflared[source]
Bases:
objectManager for cloudflared binary and tunnel processes.
Handles: - OS detection - Binary download and caching - Tunnel process management - Log capture
- DOWNLOAD_URLS = {'darwin': 'https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-darwin-amd64', 'linux': 'https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64', 'windows': 'https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-windows-amd64.exe'}
- get_binary_path()[source]
Get path to cloudflared binary, downloading if necessary.
- Return type:
- Returns:
Path to cloudflared binary
- Raises:
CloudflaredError – If download or setup fails
- is_running()[source]
Check if tunnel process is running.
- Return type:
- Returns:
True if running, False otherwise
Utility Functions
Utility functions for hostify.
- hostify.utils.get_home_dir()[source]
Get user’s home directory.
- Return type:
- Returns:
Path to home directory
- hostify.utils.is_port_in_use(port, host='127.0.0.1')[source]
Check if a port is in use by trying to connect to it.
Type Hints
All functions and methods in Hostify include type hints for better IDE support and type checking.
Example with type hints:
from hostify import Host
from typing import Optional
def create_host(
domain: str,
port: Optional[int] = None,
path: Optional[str] = None
) -> Host:
return Host(domain=domain, port=port, path=path)
host = create_host("app.example.com", port=3000)
host.serve()