Alodium Docs

Python example

Integrate the CLI into a Python sensor-journal application

This tutorial builds your own small application using alodium: it creates a sensor reading, submits it through Alodium Cloud, and saves a verifiable chain receipt. The application is a Python command-line program, with no third-party Python dependencies. The same pattern can be used behind a web service or scheduled job.

Alodium Cloud has not launched publicly. Access is currently limited to internal testing. Contact the platform team to obtain an App API key and test access. This tutorial uses the App-key workflow: alodium auth login stores a supplied key locally and alodium auth status verifies it with Cloud. Current source builds also support personal browser sign-in, covered separately in the personal access guide. Personal login does not replace this application's App context or register a personal DID.

The platform team provisions the Cloud App used to authenticate this application.

1. Prepare tools and access

Install Python 3.10+ and CLI v0.1.2 or later from the installation guide. No repository clone or Go installation is required. Verify your tools:

alodium version
python3 --version

Ask the platform administrator for all of the following:

  • A reachable Cloud base URL and an App API key with app:read/app:write.
  • An existing, eligible chain task's business ID, not its bytes32 chain ID.
  • The contributor's decimal DID ID and the publisher DID your application uses.

The task must exist before anchoring; this example does not create tasks or register DIDs. App membership does not require a contributor-DID binding on the current managed anchor endpoint. The admin provisioning appendix below documents how the App and key are created.

2. Authenticate your application

Replace this example URL with the administrator's URL:

alodium profile add sensor-demo --url https://cloud.example.com
alodium profile use sensor-demo
alodium auth login
alodium auth status --output json

Paste the App key into the hidden login prompt. Confirm that app_id identifies your App. Login stores the key; the status call verifies it against Cloud. For a container, inject XNY_API_URL and XNY_API_KEY from a secret store instead. The demo inherits the same environment/profile as alodium. An exported URL/key will override the profile, so remove stale overrides when switching Apps.

3. Create your first reading

Create an application directory and download the example. It requires Python's standard library and the installed alodium binary.

mkdir my-sensor-app
cd my-sensor-app
curl -fL https://docs.alodium.xyz/examples/sensor-journal/demo.py -o demo.py
python3 demo.py --help

Continue from this directory for all remaining commands. The downloaded demo.py is your starting application source: read it and adapt its simulated sensor producer to your own data. Replace the task, contributor and publisher values below with those provided to you. Use a new event directory:

python3 demo.py prepare \
  --directory ./runs/reading-001 \
  --task-id REPLACE_WITH_TASK_BUSINESS_ID \
  --contributor-did-id 12345 \
  --publisher did:alodium:my-sensor-app

This step performs no network request. It generates a sample sensor payload, contribution metadata and durable local state containing an idempotency key. Treat the sample reading as demonstration data, not a real measurement. For your own application, replace the sample producer with a sensor, form or import job. Keep each prepared reading immutable once submission starts.

The metadata uses kind: SAMPLE, verdict: 0 and grade: 0. Verdict zero means PENDING and grade zero means NONE (unreviewed). Grade is an enum from 0 to 5, not a percentage. published_at is generated in RFC3339 format. Cloud uploads the payload and constructs storage metadata.

4. Submit through the CLI

python3 demo.py submit \
  --directory ./runs/reading-001

The program invokes alodium anchor push --no-wait --output json, passing the two files and the saved idempotency key. It saves the returned job ID locally. Acceptance is asynchronous: a successful submit does not prove finalization.

If the network connection fails after Cloud accepts the request, rerun the same command with the same directory. The saved key and unchanged files let Cloud recognize the retry. Do not create a new directory/key merely because a response was lost. Cloud retains replayable idempotency records for 24 hours. The demo conservatively refuses submit once the event is 24 hours old. After that, query the saved job or ask the operator to resolve an uncertain outcome. Create a fresh directory when you intentionally submit a new reading.

5. Inspect and keep the receipt

python3 demo.py status \
  --directory ./runs/reading-001

Run status again while the operation is pending. finalized is the successful terminal state. Keep the returned job ID, on-chain entity ID, chain ID and transaction hash with your application's record. Failed terminal states (reverted, rejected, cancelled) produce a nonzero exit while preserving the operation for diagnosis. A successful status query for a pending job does not mean the reading is anchored yet.

The demo saves the latest operation in receipt.json and keeps its recovery state in state.json. An early receipt is a status snapshot; use its status field to distinguish it from final evidence. If submit never returned a job ID, retry submit before querying status.

6. Run the demo without Cloud credentials

Download the optional integration test into the same application directory. It starts a local mock Cloud server and drives your installed CLI:

curl -fL https://docs.alodium.xyz/examples/sensor-journal/test_demo.py -o test_demo.py
XNY_BIN="$(command -v alodium)" python3 -m unittest discover -s . -p 'test_demo.py' -v

This verifies the local application/CLI interaction without live credentials or chain writes. It does not prove a deployment or live transaction. For live acceptance, complete steps 2–5 with your environment and confirm finalized.

Adapt it into your application

Keep the same sequence: create an immutable business record, persist its idempotency key, submit, save the job ID, then reconcile status until terminal. For multiple users or workers, move the local files/state into durable storage and serialize updates to each contribution. The example is a single-process teaching application, not a concurrent queue or production database.

Keep keys on the trusted application server. A browser/mobile client must not embed an App API key. Do not expose the administrator bootstrap token to your application or use it as XNY_API_KEY.

Administrator appendix: provision the Cloud App

An authorized platform administrator calls these Cloud endpoints with Authorization: Bearer <admin bootstrap token>. Source the token from the administrator's secret store; do not paste it into a shell command, tutorial recording or source file. These operations are not CLI commands.

Create the App with POST /admin/v1/apps/create:

{
  "name": "sensor-journal",
  "did": "did:alodium:my-sensor-app",
  "custody_mode": "external"
}

The response envelope is data; read the new decimal ID from data.appID (capital ID). Creating this record does not register a chain DID or create a task. Then issue the key using POST /admin/v1/apps/apikeys/create:

{
  "app_id": "REPLACE_WITH_RETURNED_APP_ID",
  "name": "sensor-journal-development",
  "scopes": ["app:read", "app:write"],
  "credential_class": "app"
}

Read the plaintext credential from data.apiKey. It is returned only once; transfer it using your secret manager and do not print the full response in a demo. The key ID is data.keyID. Requests use snake_case; these response fields use camelCase with capital ID. An App key cannot call these admin endpoints.

Last updated