When building integrations with the Cognite Python SDK, you might not always have access to a live Cognite Data Fusion (CDF) project — especially when prototyping, testing, or running demos offline. A mock server lets you simulate CDF’s API responses so you can develop confidently without touching production data.
You’ll need:
pip install cognite-sdk flask
cognite-sdk – The official Python SDK for Cognite Data Fusion.flask – A lightweight Python web framework to simulate CDF endpoints.Here’s a minimal Flask-based mock server to mimic a subset of CDF APIs.
from flask import Flask, jsonify, request
app = Flask(__name__)
# Example asset data
mock_assets = [
{"externalId": "PUMP-101", "name": "Feed Pump", "id": 1},
{"externalId": "VALVE-202", "name": "Control Valve", "id": 2}
]
@app.route("/api/v1/projects/<project>/assets", methods=["GET"])
def list_assets(project):
return jsonify({"items": mock_assets})
@app.route("/api/v1/projects/<project>/assets", methods=["POST"])
def create_asset(project):
data = request.json
mock_assets.append(data)
return jsonify(data), 201
if __name__ == "__main__":
app.run(port=5000)
This mock server responds to /assets GET and POST requests with predictable data.
When initializing the SDK, point it to your mock server instead of the real CDF API.
from cognite.client import CogniteClient
client = CogniteClient(
api_key="dummy-api-key",
project="mock-project",
base_url="http://localhost:5000/api/v1" # Override base URL
)
# List assets
assets = client.assets.list()
print(assets)
# Create a new asset
new_asset = client.assets.create({"externalId": "TANK-303", "name": "Storage Tank"})
print(new_asset)
request.args in Flask to simulate filtering, paging, etc.You can integrate this mock server into automated tests:
import pytest
from cognite.client import CogniteClient
@pytest.fixture
def client():
return CogniteClient(api_key="test", project="mock-project", base_url="http://localhost:5000/api/v1")
def test_asset_list(client):
assets = client.assets.list()
assert len(assets) > 0
assert assets[0].external_id == "PUMP-101"
Run with:
pytest
Using a mock server with the Cognite Python SDK allows you to:
When ready, you can simply change the base_url back to the real CDF endpoint and test against live data.
Read More: eAI: The Future of Digital Twins for P&IDs