Network & Remote Access

Serve CogDB graphs over HTTP and use the REST API.

CogDB includes built-in HTTP server capabilities for remote graph access.


Serving a Graph

from cog.torque import Graph

g = Graph(graph_name="social")
g.put("alice", "knows", "bob")
g.put("bob", "knows", "charlie")

g.serve()  # Start on port 8080

Server Options

g.serve(
    port=8080,
    host="0.0.0.0",
    blocking=False,
    writable=False
)

Multiple Graphs

g1 = Graph(graph_name="users")
g2 = Graph(graph_name="products")

g1.serve(port=8080)
g2.serve(port=8080)
# http://localhost:8080/users/
# http://localhost:8080/products/

Stopping

g.stop()

# Or stop all:
from cog.server import stop_server
stop_server(8080)

Connecting Remotely

from cog.torque import Graph

remote = Graph.connect("http://192.168.1.5:8080/social")
result = remote.v("alice").out("knows").all()
# {'result': [{'id': 'bob'}]}

REST API

MethodPathDescription
GET/List all graphs
GET/{graph}/statsJSON statistics
POST/{graph}/queryExecute query
POST/{graph}/mutateWrite operations

Query API

curl -X POST http://localhost:8080/social/query \
  -H "Content-Type: application/json" \
  -d '{"q": "v(\"alice\").out(\"knows\").all()"}'

Stats API

curl http://localhost:8080/social/stats

Mutate API

Requires writable=True.

curl -X POST http://localhost:8080/social/mutate \
  -H "Content-Type: application/json" \
  -d '{"op": "put", "args": ["charlie", "knows", "diana"]}'

Security

The server is for trusted networks. For production, add authentication via a reverse proxy.

Built-in protections:

  • Method whitelisting
  • Dunder blocking
  • Read-only by default
  • Restricted eval

On this page