Getting started

Add uftp, build a signed FlexRequest, receive it with an Endpoint, and answer it, in one short Rust program.

On this page

If DSO, aggregator, ISP or congestion point are new, read What UFTP is first.

Add the crate#

[dependencies]
uftp = "0.3"
# For an HTTP endpoint on the `http` 1.x types (axum, hyper, reqwest, ureq):
# uftp = { version = "0.3", features = ["http"] }
# Only the messages, their rules and XML, without keys or signing:
# uftp = { version = "0.3", default-features = false }

Rust 1.88 or later. No build has an async runtime or a network crate.

FeatureDefaultAdds
cs1onkeys, seal and open, the directory, Endpoint, outbox and replay guard
httpoffthe http 1.x mapping; turns on cs1

One request, end to end#

A DSO asks an aggregator for 500 kW less consumption tomorrow from 17:00 to 19:00. The aggregator verifies the request and accepts it, and the DSO reads the answer. The full program is examples/flex_request.rs in the repository: cargo run --example flex_request.

use uftp::*;

// Keys come from your key store; fixed seeds keep the example short.
let dso = SigningKey::from_seed(&[1; 32]);
let agr = SigningKey::from_seed(&[2; 32]);
let dso_domain: InternetDomain = "dso.example".parse()?;
let agr_domain: InternetDomain = "agr.example".parse()?;

// Where receivers find senders' public keys (DNS in production, see Discovery).
let directory = Directory::new()
    .with(Participant::new(dso_domain.clone(), Role::Dso, dso.public_keys([0; 32])))
    .with(Participant::new(agr_domain.clone(), Role::Agr, agr.public_keys([0; 32])));

// 1. The request. A conversation starts with its first message; ISPs are 15 minutes.
let meta = Metadata::new(dso_domain.clone(), agr_domain.clone(), now, id(1));
let header = FlexHeader::new(meta, "Europe/Amsterdam".parse()?, "2026-12-01".parse()?,
                             "ean.871685900012636543".parse()?);
let isps = vec![FlexRequestIsp::requested(IspSpan::new(69, 8)?, -500_000, 0)];
let request = FlexRequest::new(header, "2026-11-30T18:00:00Z".parse()?, isps);
let document = seal(&request.into(), Role::Dso, &dso)?.to_xml();

// 2. The aggregator opens it: signature, schema, sender, role, rules, recipient.
let opened = open(document.as_bytes(), &OpenOptions::for_recipient(agr_domain), directory.lookup())?;
let received: FlexRequest = opened.into_message().try_into()?;

// 3. It accepts, in the same conversation.
let response = received.reply(Reply::accepted(id(2), now));
let answer = seal(&response.into(), Role::Agr, &agr)?.to_xml();

seal refuses to sign a message that breaks a rule, so what you send is always valid.

open verifies the signature before it parses anything.

A real endpoint#

A real receiver needs more than open: the market's ISP length and time zone, the time rules, replay protection, and a check that a response answers something you sent. Endpoint runs them all, in the right order:

let mut endpoint = Endpoint::new(OpenOptions::for_recipient(agr_domain), SignedDuration::from_mins(15))
    .with_market(Market { isp_duration: IspDuration::PT15M, time_zone: "Europe/Amsterdam".parse()? })
    .with_outbox(Outbox::new(SignedDuration::from_hours(24)));

match endpoint.receive(body, now, directory.lookup()) {
    Ok(opened) => { /* your business logic */ }
    Err(e) => {
        // e.http_status() answers the POST. Where the specification wants a business
        // answer, e.response(id, now) is the signed Rejected response to send back.
        if let Some(answer) = e.response(new_id(), now) { /* seal and send */ }
    }
}

examples/loopback.rs runs a whole trade this way: request, offer, order, a replayed order, and a settlement the aggregator checks item by item, each answered.

Next#

  • Receiving: what each step checks, and the HTTP mapping.
  • Messages: all 26 types, constructors and flow helpers.
  • Settling: the month's settlement, built and checked.
  • Interoperability: before you connect to a Shapeshifter deployment.