Skip to main content

HTTP Services

Morph Cloud allows you to expose HTTP services running within your instances to the public internet. This makes it easy to share web applications, APIs, or any service running on an HTTP port.

Note: When you create a snapshot from an instance with running HTTP services, those services will continue to run when new instances are started from that snapshot. This is because snapshots preserve both memory and disk state, including all running processes.

Expose an HTTP Service

To make an HTTP service accessible publicly, you can use the expose_http_service method. You need to specify a service name and the port the service is listening on inside the instance.

service_url = instance.expose_http_service(name="my-service", port=8080)
print(f"HTTP Service exposed at: {service_url}")

Secure HTTP Service with API Key Authentication

You can enable API key authentication for your exposed services for enhanced security.

service_url = instance.expose_http_service(name="secure-service", port=8081, auth_mode="api_key")

When auth_mode="api_key" is enabled, requests to the service URL must include the Authorization: Bearer YOUR_MORPH_API_KEY header:

import requests

headers = {"Authorization": f"Bearer {client.api_key}"}
response = requests.get(service_url, headers=headers)

Hide an HTTP Service

To stop exposing an HTTP service, use the hide_http_service method.

instance.hide_http_service(name="my-service")

List Exposed HTTP Services

You can view all currently exposed HTTP services for an instance through the networking.http_services property.

# Access HTTP services through the networking property
for service in instance.networking.http_services:
print(f"Service Name: {service.name}")
print(f"URL: {service.url}")
print(f"Internal Port: {service.port}")

The CLI commands will show HTTP services in the following formats:

  • morphcloud instance get shows detailed JSON output including all HTTP services
  • morphcloud instance list shows a table with a column for HTTP services in the format name:port, name2:port2, for all active instances

Complete Example: Authenticated HTTP Service

Here's a complete example showing how to set up an HTTP server, expose it with authentication, and make authenticated requests.

from morphcloud.api import MorphCloudClient
import time
import requests
from http.server import HTTPServer, BaseHTTPRequestHandler

# Start a simple HTTP server within the instance
client = MorphCloudClient()
instance_id = "morphvm_abc123" # Replace with your instance ID
instance = client.instances.get(instance_id=instance_id)

# Create a simple HTTP server script
server_script = """
from http.server import HTTPServer, BaseHTTPRequestHandler

class SimpleHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(b'Hello from Morph Cloud!')

server = HTTPServer(('0.0.0.0', 8081), SimpleHandler)
print('Server started on port 8081')
server.serve_forever()
"""

# Write the server script to the instance
instance.exec("mkdir -p /app")
instance.write_file("/app/server.py", server_script)

# Start the server in the background
instance.exec("cd /app && python3 server.py &")
time.sleep(2) # Give the server time to start

# Expose the HTTP service with API key authentication
service_url = instance.expose_http_service(name="secure-service", port=8081, auth_mode="api_key")
print(f"Secure HTTP Service exposed at: {service_url}")

# Make an authenticated request to the service
headers = {"Authorization": f"Bearer {client.api_key}"}
response = requests.get(service_url, headers=headers)
print(f"Response: {response.status_code} {response.text}")

# Clean up when done
instance.hide_http_service(name="secure-service")
print("Service hidden")