Skip to main content

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​

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}")

Exposing Services with API Key Authentication​

For secure services, you can require API key authentication:

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")

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:

# Access a service with API key authentication
curl -H "Authorization: Bearer YOUR_API_KEY" https://your-service-url.morphcloud.com

Service Authentication Management​

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()

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​

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.

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.")

List Exposed HTTP Services​

You can get a list of all currently exposed HTTP services for an instance.

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("---")