HTTP Services
HTTP Services
Expose HTTP Services
You can expose HTTP services running inside your instance to the outside world using the expose API. Services can be exposed with different authentication modes for access control.
Basic Service Exposure
- Python
- TypeScript
- CLI
from morphcloud.api import MorphCloudClient
client = MorphCloudClient()
instance_id = "instance_your_instance_id" # Replace with a valid instance ID
instance = client.instances.get(instance_id=instance_id)
# Expose a service running on port 8080 inside the instance with API Key Authentication
service_info = instance.expose_http_service(
name="my-web-app",
port=8080
)
print(f"Service URL: {service_info.url}")
print(f"Service Name: {service_info.name}")
print(f"Internal Port: {service_info.port}")
print(f"Auth Mode: {service_info.auth_mode}")
import { MorphCloudClient } from 'morphcloud'; // adjust import path if needed
const client = new MorphCloudClient();
async function exposeService() {
const instance_id = "instance_your_instance_id"; // Replace with a valid instance ID
const instance = await client.instances.get({ instance_id: instance_id });
// Expose a service running on port 8080 inside the instance (no authentication)
const serviceInfo = await instance.exposeHttpService("my-web-app", 8080);
console.log(`Service URL: ${serviceInfo.url}`);
console.log(`Service Name: ${serviceInfo.name}`);
console.log(`Internal Port: ${serviceInfo.port}`);
console.log(`Auth Mode: ${serviceInfo.auth_mode}`);
}
exposeService();
# Expose a service running on port 8080 inside the instance (no authentication)
morphcloud instance expose-http instance_your_instance_id my-web-app 8080
# Expose with specific authentication mode
morphcloud instance expose-http instance_your_instance_id my-web-app 8080 --auth-mode none
Exposing Services with API Key Authentication
For secure services, you can require API key authentication:
- Python
- TypeScript
- CLI
from morphcloud.api import MorphCloudClient
client = MorphCloudClient()
instance_id = "instance_your_instance_id" # Replace with a valid instance ID
instance = client.instances.get(instance_id=instance_id)
# Expose a service with API key authentication required
secure_service = instance.expose_http_service(
name="secure-api",
port=3000,
auth_mode="api_key"
)
print(f"Secure Service URL: {secure_service.url}")
print(f"Auth Mode: {secure_service.auth_mode}")
print("Note: API key required for access")
import { MorphCloudClient } from 'morphcloud';
const client = new MorphCloudClient();
async function exposeSecureService() {
const instance_id = "instance_your_instance_id"; // Replace with a valid instance ID
const instance = await client.instances.get({ instance_id: instance_id });
// Expose a service with API key authentication required
const secureService = await instance.exposeHttpService("secure-api", 3000, "api_key");
console.log(`Secure Service URL: ${secureService.url}`);
console.log(`Auth Mode: ${secureService.auth_mode}`);
console.log("Note: API key required for access");
}
exposeSecureService();
# Expose a service with API key authentication
morphcloud instance expose-http instance_your_instance_id secure-api 3000 --auth-mode api_key
Authentication Modes
HTTP services support two authentication modes to control access:
None Authentication (Default)
- Public Access: No authentication required
- Use Cases: Public websites, demo applications, development servers
- Security: Services are publicly accessible via the provided URL
API Key Authentication
- Protected Access: Requires valid API key in request headers
- Use Cases: Private APIs, secured services, production applications
- Security: Only requests with valid API keys can access the service
Using API Key Authentication
When a service is exposed with api_key authentication mode, clients must include your API key in requests:
- curl
- Python
- TypeScript
# Access a service with API key authentication
curl -H "Authorization: Bearer YOUR_API_KEY" https://your-service-url.morphcloud.com
import requests
# Access a service with API key authentication
headers = {
"Authorization": "Bearer YOUR_API_KEY"
}
response = requests.get("https://your-service-url.morphcloud.com", headers=headers)
print(response.text)
// Access a service with API key authentication
const response = await fetch("https://your-service-url.morphcloud.com", {
headers: {
"Authorization": "Bearer YOUR_API_KEY"
}
});
const data = await response.text();
console.log(data);
Service Authentication Management
- Python
- TypeScript
from morphcloud.api import MorphCloudClient
client = MorphCloudClient()
def manage_service_security():
"""Example of managing HTTP service authentication"""
instance_id = "instance_your_instance_id"
instance = client.instances.get(instance_id=instance_id)
# Expose different services with different security levels
# 1. Public development server (no auth)
dev_service = instance.expose_http_service(
name="dev-server",
port=3000,
auth_mode="none"
)
print(f"Development server: {dev_service.url} (Public)")
# 2. Secure API endpoint (API key required)
api_service = instance.expose_http_service(
name="api-v1",
port=8080,
auth_mode="api_key"
)
print(f"API endpoint: {api_service.url} (API Key Required)")
# 3. Admin interface (API key required)
admin_service = instance.expose_http_service(
name="admin-panel",
port=9000,
auth_mode="api_key"
)
print(f"Admin panel: {admin_service.url} (API Key Required)")
manage_service_security()
import { MorphCloudClient } from 'morphcloud';
const client = new MorphCloudClient();
async function manageServiceSecurity() {
const instance_id = "instance_your_instance_id";
const instance = await client.instances.get({ instance_id: instance_id });
// Expose different services with different security levels
// 1. Public development server (no auth)
const devService = await instance.exposeHttpService("dev-server", 3000);
console.log(`Development server: ${devService.url} (Public)`);
// 2. Secure API endpoint (API key required)
const apiService = await instance.exposeHttpService("api-v1", 8080, "api_key");
console.log(`API endpoint: ${apiService.url} (API Key Required)`);
// 3. Admin interface (API key required)
const adminService = await instance.exposeHttpService("admin-panel", 9000, "api_key");
console.log(`Admin panel: ${adminService.url} (API Key Required)`);
}
manageServiceSecurity();
Security Best Practices
When to Use API Key Authentication
✅ Recommended for:
- Production APIs
- Admin interfaces
- Internal services
- Services handling sensitive data
- Services with rate limiting needs
When to Use No Authentication
✅ Suitable for:
- Public websites
- Demo applications
- Development/testing environments
- Static content serving
- Public documentation sites
API Key Security Guidelines
- Rotate Keys Regularly: Change API keys periodically for security
- Environment Variables: Store API keys in environment variables, not code
- Principle of Least Privilege: Use API keys with minimal required permissions
- Monitor Usage: Track API key usage for suspicious activity
- Secure Transmission: Always use HTTPS when transmitting API keys
Service Management Examples
- Python
def audit_service_security():
"""Audit all exposed services for security compliance"""
instances = client.instances.list()
for instance in instances:
if instance.status == "ready":
print(f"\n=== Instance: {instance.id} ===")
# Get all HTTP services for this instance
if hasattr(instance.networking, 'http_services'):
services = instance.networking.http_services
for service in services:
security_status = "🔒 SECURE" if service.auth_mode == "api_key" else "🌐 PUBLIC"
print(f"{security_status} {service.name} (port {service.port})")
print(f" URL: {service.url}")
print(f" Auth: {service.auth_mode}")
# Security recommendations
if service.auth_mode == "none":
print(" ⚠️ Consider adding API key auth for production")
print()
audit_service_security()
Hide HTTP Services
When you no longer need a service to be exposed, you can hide it.
- Python
- TypeScript
- CLI
from morphcloud.api import MorphCloudClient
client = MorphCloudClient()
instance_id = "instance_your_instance_id" # Replace with a valid instance ID
instance = client.instances.get(instance_id=instance_id)
# Hide a previously exposed service
instance.hide_http_service(name="my-web-app")
print("Service has been hidden.")
import { MorphCloudClient } from 'morphcloud'; // adjust import path if needed
const client = new MorphCloudClient();
async function hideService() {
const instance_id = "instance_your_instance_id"; // Replace with a valid instance ID
const instance = await client.instances.get({ instance_id: instance_id });
// Hide a previously exposed service
await instance.hideHttpService({
name: "my-web-app"
});
console.log("Service has been hidden.");
}
hideService();
# Hide a previously exposed service
morphcloud instance hide-http instance_your_instance_id my-web-app
List Exposed HTTP Services
You can get a list of all currently exposed HTTP services for an instance.
- Python
- TypeScript
- CLI
from morphcloud.api import MorphCloudClient
client = MorphCloudClient()
instance_id = "instance_your_instance_id" # Replace with a valid instance ID
instance = client.instances.get(instance_id=instance_id)
# Get all exposed HTTP services
services = instance.get_http_services()
for service in services:
print(f"Service Name: {service.name}")
print(f"URL: {service.url}")
print(f"Internal Port: {service.port}")
print("---")
import { MorphCloudClient } from 'morphcloud'; // adjust import path if needed
const client = new MorphCloudClient();
async function listServices() {
const instance_id = "instance_your_instance_id"; // Replace with a valid instance ID
const instance = await client.instances.get({ instance_id: instance_id });
// Get all exposed HTTP services
const services = await instance.getHttpServices();
services.forEach(service => {
console.log(`Service Name: ${service.name}`);
console.log(`URL: ${service.url}`);
console.log(`Internal Port: ${service.port}`);
console.log("---");
});
}
listServices();
# List all exposed HTTP services for an instance
morphcloud instance list-http instance_your_instance_id