GRIETENS
Menu
Grietens

Connect a quote engine to Grietens

Updated

The question this guide answers
How do I connect the quoting service of a shop to Grietens so it can be asked for quotes?

Start with the recommended setup at https://app.grietens.cloud/kit/setup.md, also as a page at https://app.grietens.cloud/kit/setup. It is one checklist for an AI agent or a developer, every step in order with an example and a way to check it, from the engine card to a finished listing. This guide explains why things work the way they do.

Many shops that make things to order already have a way to price a job. It may be a roof truss calculator, a panel configurator, a spreadsheet with a small web form in front of it, or a person who answers a request by hand. In Grietens such a service is called a quote engine. This page explains how a developer, or an AI that works for the shop, puts one on the Grietens network in one step, what Grietens sends to it, how both sides prove who they are and what the shop gets.

The engine stays the own service of the shop. Grietens never logs in to it and never changes anything in its systems. It only calls the address it was given and keeps what the engine answers.

The short version

Joining is giving Grietens one address. The engine answers a small description of itself at that address, called the engine card, and it signs what it sends with its own key. Grietens reads the card, checks that the address really holds that key, makes the account of the shop and switches on its own quote link. There is no sign-in, no approval in a browser and no secret to copy from one place to another.

POST https://app.grietens.cloud/api/public/join
{"url": "https://quotes.example.com/quote"}

The same call exists as the tool join_network on the public MCP server of Grietens at https://app.grietens.cloud/api/v1/network/mcp, which needs no login, and as a page with one field at https://grietens.cloud/join. An engine built with the kit below can also make the call by itself when it starts.

Step 1. Describe the engine

At the address where Grietens will send requests, a GET request returns the engine card as JSON. Four fields are required. The name of the shop, the product words it can quote, the version of the format and the public key of the engine. The others help buyers and are optional.

{ "grietens_engine": "2", "name": "Kalns trusses", "products": ["roof_trusses", "wall_panels"],
  "town": "Aizpute", "country": "LV", "language": "lv", "about": "Roof trusses for houses and barns",
  "website": "https://kalns.example.com", "delivery_radius_km": 200, "open_requests": true,
  "public_key": "<the raw 32-byte Ed25519 public key in base64url>" }

The card is public and is not signed. Version 2 is the format with the signatures described below, and a card of version 1 is refused. The text of the card is checked like anything a person types. A card that holds what looks like a password or a key, or the name of a worker, is refused. The address must be a public https address on the standard port, and its path must be plain ASCII with no dot segments and no needless percent escapes, so that every program reads the same path. A host name with letters such as ķ or ē must be written in its xn-- form, and an address with a space, a tab or a line break anywhere is refused rather than trimmed. Private networks, IP addresses and the address of Grietens itself are refused, and every later call checks the resolved address again before it connects.

The optional field logo is the https address of a PNG, JPEG or WebP picture of at most 512 KB on the host of the engine. Grietens fetches it when the shop joins and keeps its own copy, and buyers see it on the card of the shop and on its own quote page. An SVG is refused, because it can carry a script. A shop without a logo is shown with its initials. In the kit a local file is enough as the logo, and the kit serves it. The owner or his AI can also set the logo later with set_shop_logo, and a logo set that way is never replaced by the card.

Step 2. The keys

Each side has its own Ed25519 key pair and keeps the private half to itself. The public key of the engine is on its card. The public keys of Grietens are published at https://app.grietens.cloud/.well-known/grietens-keys.json.

A key never signs bare bytes. Every signature covers a short text of lines that names the version of the scheme, who signs, the purpose of the call, the method, the path, the address of the engine in one fixed form, the time in unix seconds, a random nonce and the SHA-256 hash of the body. A signature made for one purpose therefore never counts for another, and a signature made for one engine never counts at another address. The exact texts are in the documentation of the quote network, and both kits build them for you.

Every request from Grietens carries five headers. X-Grietens-Key-Id names the key, X-Grietens-Timestamp is the time, X-Grietens-Nonce is the nonce, X-Grietens-Purpose says whether the call is a hello, a quote request or a check, and X-Grietens-Signature-Ed25519 is the signature. The engine rebuilds the text from the request it received, checks it with the published key of Grietens for its own address, refuses a timestamp that is more than five minutes away from its own clock and refuses a nonce it has seen in the last five minutes.

The engine signs only its answers to calls that passed this check, and its callbacks, with its own key in the headers X-Engine-Timestamp and X-Engine-Signature-Ed25519. It never signs the card, an error or an answer to a call that failed the check. An answer is bound to the request it answers, so nobody can borrow the key of the engine to sign something else. Grietens checks those signatures against the key it saw on the card the first time.

Step 3. The hello

Before Grietens makes anything, it proves that the address controls the key on the card. It sends a signed request of type hello with a random nonce. The engine answers at once with the same nonce, signed with its key for that hello. A hello is never a quote request, and an engine must not treat it as one.

The hello carries nothing else. It never carries the sign-up link of the account, because it goes out before the answer of the engine has been checked, and the join answer never carries it either, because anyone can type the address of someone else.

What the shop gets

The account exists from the moment the shop joins. It receives requests, keeps every answer as its record and has its own quote link, such as https://grietens.cloud/q/kalns-trusses, which works at once. A request made on that link goes to this engine only.

A few seconds after the shop joins, Grietens labels it with one to three categories from its public list, such as timber construction or sheet metal and laser cutting. A small AI chooses them from what the card says and from the product words of the engine, and when that AI is not available a plain word match does it. An engine that takes open requests from buyers then also receives requests that name none of its product words but belong to one of those categories, once the company has been checked. Such a request comes without a product hint, and its notes say which category brought it. The engine declines what the shop does not make. A label the owner sets is never changed by Grietens. The list of categories is at https://app.grietens.cloud/api/public/categories.

The shop described itself, and controlling an address is not the same as being the company. So the page of the shop says plainly near the top that the company described itself and has not been checked by Grietens. Search engines are asked not to index that page and it is left out of the sitemap. Grietens does not send the shop requests from its own public request page or from the AI apps of buyers until a person at Grietens has checked the company in the register. This holds for every shop, also one whose account was made another way. When the card says open_requests is true, the shop receives those requests from that moment on. The drawings of a buyer never go to an unchecked party.

The data terms

Joining accepts the Grietens Data Terms, which are at https://app.grietens.cloud/terms. Under section 7A of the Data Licence Annex each quote the shop gives through Grietens is counted, as numbers only, in anonymous market price figures. Those numbers are the kind and size of the job, the price, the lead time, the region of the buyer and the month. The name of the shop, the buyer, the files and the words of a request never go in, and a figure is shown only when at least five shops and ten quotes stand behind it.

A machine cannot agree to anything, so the acceptance is on record only when the person who runs the engine reads the terms and then puts the version on the engine card, as "terms": "1.2". Neither kit sets it by default. In Node it goes in the card options, card: { name, products, terms: '1.2' }, and in Python in the card dictionary, card={"name": ..., "terms": "1.2"}. At the join the kit logs one line that says whether the card accepts the terms and what follows. The answer of the join carries terms with the current version, whether the account has it on record and a plain note.

A card without the field joins exactly as before and works the same way. Its account simply has not accepted the terms on record yet, so its quotes are not counted in the price figures until the field is on the card or the owner accepts the terms after signing in. A quote given before the acceptance is never counted.

The sign-up link and why there is nothing to keep

The sign-up link adds a login, an email and a password, to the account that already exists. Everything the shop received before the claim is there after it. An account without a login is a normal state that can last for ever, and a shop that ever received a request or a quote is never deleted for being unclaimed.

There is nothing to store except the key file of the engine. The key of the engine is the identity of the shop on Grietens. The engine gets the sign-up link only as the answer to its own signed call POST /api/public/engine/sign-in-link. The kit makes that call only when the developer asks for it with signInLink(), and it returns the link to the caller without ever printing it, because the log of a hosted service is often read by other people. The engine can ask again at any time. A new link makes the old one stop working. The AI that set up the account can also ask for the link, and the team of Grietens can issue one after checking who the owner is. Grietens sends no email and has no address of the shop before the claim. The account has a login from the moment its owner sets a password. From then on the sign-in link and the workspace key calls answer one sentence and give nothing, every key the engine asked for itself stops working when the claim is complete, and the owner signs in with his own password. Reports of quotes keep working.

Step 4. Requests, answers and callbacks

A request is a POST with a JSON body. It names the product, the quantity, the date by which it is needed, notes and files, and a callback address. When Grietens knows the language of the page or the request of the buyer, the body also carries language, which is "lv" or "en", so the engine can write its messages and its live updates in that language. The field is optional and is left out when the language is not known, it is never guessed, and an engine that ignores it works exactly as before. It is part of the signed body like every other field. The call waits at most ten seconds. The engine answers with one of four status words. needs_review means here is the figure and needs a price, a three letter currency code and at least one line of scope. pending means the quote follows later at the callback address, which stays valid for thirty days. declined and failed close the request with the message of the engine. The optional field kind says whether the figure is an estimate or a quote.

A signed answer from the engine is shown as the final word of the shop. A callback that is not signed with the key of the engine is kept but never shown by itself, so a leaked callback address alone can never publish a price. When nobody answers a call, Grietens tries again after about 20 seconds, 2 minutes and 10 minutes. When an engine leaves three requests in a row unanswered after every retry, Grietens pauses it and the link page of the shop says it is not taking requests right now. The next successful hello switches it on again.

An answer may also carry the documents of the shop, such as an offer as a PDF, a drawing or a picture. The field files lists them, each with a name and an https address on the host of the engine itself. That means the same host name as the address of the engine, on port 443, at any path on that host. A file does not have to sit under the path of the engine, so an address such as https://quotes.example.com/f/ticket/offer.pdf is as good as one under /quote. Grietens follows a redirect only when it is a 307 or 308 to the same host. Right after the quote arrives Grietens fetches each file once and keeps its own copy, so the buyer can open it even when the engine is offline, and the file cannot change after the quote. What the bytes are decides how a file is shown. PDF, PNG, JPEG and WebP files are shown to the buyer, drawings such as DXF, DWG, IFC and STEP are offered as downloads, and programs, archives, web pages and SVG files are refused whatever their name says. At most five files of 10 MB each and 25 MB together are kept. A file that cannot be kept is recorded with the reason and never holds the quote back. The shop sees the same files, and the photos and drawings the buyer sent, in the Quotes tab of its account. In the kit a file is one line with attach, which serves a local file at an unguessable address on the engine itself.

The kit

Two small files leave only the pricing to the developer. Both are plain files to copy into the project of the engine. Fetch them from https://app.grietens.cloud/kit/grietens-engine.mjs and https://app.grietens.cloud/kit/grietens_engine.py. kit/grietens-engine.mjs is for Node 18 or newer and has no dependencies. kit/grietens_engine.py is for Python 3.10 or newer and needs the cryptography package. The two behave the same. Each one makes and keeps the key pair in a file the developer chooses, serves the card, checks the signatures of Grietens, answers the hello, signs only answers to calls that passed the check and its callbacks, answers pending when a quote takes longer than a few seconds and delivers it later. A check of a request the engine has never seen is answered with a plain 404, never with pending. The developer writes one function that turns a request into an answer.

import { createEngine } from './grietens-engine.mjs';
const engine = createEngine({
  url: 'https://quotes.example.com/quote', keyFile: './grietens-engine-key.pem',
  card: { name: 'Kalns trusses', products: ['roof_trusses'], town: 'Aizpute', country: 'LV' },
  async quote(request) {
    return { status: 'needs_review', kind: 'estimate', price: 18400, currency: 'EUR', scope: ['trusses'] };
  },
});
engine.listen(8787);
await engine.join();

The kit also reports quotes the shop made outside the network, asks for a key to the Grietens API of its own account while the account has no login and asks for a sign-in link. Both the key and the link are returned to the caller and never written to the log. That key reads, sends data and writes in its own account, and never starts automations, connectors or runtimes, which wait for the login of the owner. The numbers above are a made-up example, not a price list.

When a person sets the price, or the service restarts

Without a store the kit keeps requests, answers and the nonces it has seen in memory. That is fine for an engine that answers at once. A restart forgets every request, so a late quote can no longer be delivered, and a call replayed across the restart is not recognised. An engine that answers pending, sleeps when idle or restarts on every deploy gives the kit a store. fileStore(path) in Node and FileStore(path) in Python keep everything in one JSON file that is written whole on every change. A hosted service whose disk is wiped on every deploy needs a store backed by its database instead. Any object with five small methods will do, and the header of each kit lists them. On start, an engine with a store sends again every answer that was made but not delivered.

When a person releases the price hours later, quote() answers pending with a short message, and later, after any number of restarts, the engine calls complete(request_id, answer). The kit stores the answer and posts it, signed, to the callback address of that request.

const engine = createEngine({ url, keyFile, card, store: fileStore('/data/grietens.json'),
  async quote(request) { await myQueue.add(request); return { status: 'pending', message: 'our estimator prices this' }; } });
// later, when the estimator releases the price
await engine.complete(requestId, { status: 'needs_review', kind: 'quote', price: 18400, currency: 'EUR', scope: ['trusses'] });

The lower layer

An engine with its own storage, its own timing or its own web server can use the protocol functions without the built in server. createProtocol in Node and Protocol in Python verify a call from Grietens and say in plain words why one is refused, build the signed headers for an answer to a verified call, sign and send a callback or a live update for a request at any later time from the request id, the callback address and the answer alone, serve the card, join, and ask for a sign-in link, a workspace key or a quote report. The header of each kit shows how.

A call that is not signed by Grietens is refused with a 401 before the kit reads its body, and nothing of the body is repeated in the answer. The purpose of a call comes from its signed header, so a body that does not match that purpose is refused too. A kit never signs an answer that is bound to an empty request id.

Every call the kit makes goes out through one function, so an engine that guards its outgoing traffic, for example one that refuses private addresses or redirects, hands that guard to the kit instead of signing and sending by hand. In Node it is the option fetch, any function with the signature of fetch, and in Python it is transport, a function that behaves like the opener of urllib. It carries the keys of Grietens, callbacks, live updates, the join, the sign-in link, the workspace key and quote reports. The option sendTimeoutMs in Node and send_timeout_s in Python says how long a callback or a live update waits for Grietens, 15 seconds by default. That is long enough, because Grietens answers a callback in well under a second, and short enough that a live update never holds a quote up. A callback that runs out of time is tried again.

const g = createProtocol({ url, keyFile, fetch: myGuardedFetch, sendTimeoutMs: 15000 });

Coming from the first kit

The first kit made a card of version 1 and could also check a shared secret. Moving to the current kit is a rewrite of the glue around the pricing, not of the pricing itself, because quote(request) stays the same. Keep the key file, since the same key is the same shop. Three things changed.

The options of createEngine. The first kit took a shared secret and made a card of version 1. The current kit takes no secret, makes a card of version 2 and takes a store for an engine that answers later or restarts. It also no longer prints the sign-up link, and signInLink() returns a fresh one when a person wants to look inside.

// before
createEngine({ url, keyFile, card, quote, hmacSecret: process.env.GRIETENS_SECRET });
// after
createEngine({ url, keyFile, card, quote, store: fileStore('./grietens-requests.json') });

The shared secret is gone. A call from Grietens could carry a header X-Grietens-Signature-256 with an HMAC of the body under the shared secret, and every signature covered only the time and the body. Now only the key of Grietens signs, over a text that names the purpose, the method, the address of the engine, the path, the time, a nonce and the hash of the body.

// before: compare X-Grietens-Signature-256 with an HMAC of the body under the shared secret
// after
const v = await g.verify({ method, path, headers, body });

The hello. It was answered with the nonce and an hmac_proof, the HMAC of the nonce under the shared secret, and could carry the sign-up link. Now it carries only the nonce, and a key hand-over when a new key replaces an old one, signed for that hello.

// before
return { nonce, hmac_proof: 'sha256=' + hmac(secret, nonce) };
// after
return g.reply('hello', v.nonce, g.helloAnswer(v.nonce));

The checker below shows what is still done the old way.

Show the buyer the quote being made

While the engine works on a request it may send live updates to the callback address of that request, as many as it likes and as often as it likes, until the answer. The buyer watches them on his page. Each update can move a bar (a label such as Reading the drawings, a step count, a percent and the time left) and add items to a live feed, such as a short note, a picture or a file. A picture or a file is an https address on the host of the engine, and Grietens fetches and keeps it exactly like the files on an answer. An item with an id that is sent again replaces that item where it is, which suits a drawing the engine redraws every few seconds. An update is signed like a callback, with the purpose progress, and it never changes the request, the quote or the price. An update that is not signed with the key of the engine is ignored.

A picture or a file may sit at any path on the host of the engine, the same host name as its address, over https. Labels and texts read best in the language the request names in language.

Two things are recommended. Make the progress visual, with a picture of what the engine understood, redrawn under the same id as the work goes on. And send the final documents with the answer, the offer as a PDF in files, so the buyer keeps a real quote and not only a number.

In the kit it is one call, engine.progress(requestId, update), or one line per step inside the quote function.

quote: async (request, live) => {
  await live.step('Reading the drawings', { of: 2 });
  await live.show({ text: 'Roof gable, 30 degrees' }, { picture: './layout.png' });
  await live.step('Pricing');
  return { status: 'needs_review', kind: 'estimate', price: 18400, currency: 'EUR', scope: ['trusses'] };
}

The full description, with every field, the bounds, the raw signed request for an engine that does not use the kit and what the buyer sees, is the document at https://app.grietens.cloud/kit/live-updates.md. The checker below prints every live update it receives and checks that each one is signed for its request.

When a person releases the quote

Some engines answer hours later, after a person at the shop has checked the work. Such an engine answers the call with pending, and while the person checks it sends live updates with the state waiting_for_person, so the page of the buyer says the quote waits for the engineer of the factory.

The line between the two is for the engine to draw. Before the release it is sensible to show what the engine understood of the request, such as the roof type, the span and the number of trusses, and a drawing or a 3D picture of it. That tells the buyer early that the request was read right. Until the answer it should hold back any price or price range, any quantity that a price follows from, such as a volume of timber, and every internal note. Grietens shows whatever is sent the moment it arrives and cannot take a shown item back. After the release the engine sends the answer with its price by callback, and nothing after it.

// the call (after g.verify): keep what is needed later, answer pending at once
await db.save(v.requestId, { callbackUrl: v.request.callback_url, lv: v.request.language === 'lv' });
return g.reply('answer', v.requestId, { contract: '1', request_id: v.requestId, status: 'pending', message: 'Our engineer checks the quote' });
// any time later, from stored facts only: what the engine understood, never a price
await g.sendProgress({ requestId, callbackUrl: job.callbackUrl, progress: { state: 'waiting_for_person', label: 'Waiting for our engineer',
  items: [{ text: 'Gable roof, span 9.2 m, 14 trusses' }, { id: 'model', picture: 'https://quotes.example.com/f/ticket/roof.png' }] } });
// after the release: the answer by callback
await g.sendCallback({ requestId, callbackUrl: job.callbackUrl, answer: { status: 'needs_review', kind: 'quote', price: 18400, currency: 'EUR', scope: ['trusses'] } });

The live updates document has the full worked example, and the easy path does the same with pending, engine.progress and engine.complete.

Testing without Grietens

The checker is one more file, https://app.grietens.cloud/kit/grietens-engine-check.mjs. It needs Node 18 or newer and nothing else, so it runs next to any engine.

node grietens-engine-check.mjs --url http://localhost:8787/quote

It plays the part of Grietens with a throwaway key pair that it serves itself at http://127.0.0.1:8799, so start the engine with that as its Grietens address. It reads the card, sends a hello and checks that the answer is signed for that hello. It checks that the same call sent twice, a stale signature, a wrong signature and a signature for another address are refused and that a refusal is not signed. It sends a request and checks that the first answer is signed for that request, then waits for the quote by callback or by asking, and checks the signature on every check answer and on the callback. When one of them fails it says which answer it was and what it should have been bound to. It fetches every picture and file that a live update names and every file of the quote the way Grietens does, and prints for each one whether Grietens keeps it, with its kind and size, or why not and what to fix. Against an engine on localhost it compares the host with the address it was given. It sends language with the request, and --language lv switches it to Latvian. It also asks once for a request the engine never got, which should be a 404. The signed texts it builds are the ones Grietens builds, and a test in the Grietens code base compares them byte for byte. A working example engine on the kit is tools/quote-engine-example.mjs in the Grietens code base.

A shop that already has an account

A factory that already uses Grietens, for its machines for example, should not end up with a second account for its quote engine. Its owner or his AI adds the engine from inside the account instead. Two public facts replace any secret. The account names the address of the engine with the call add_quote_engine, and the engine card names the account in the field workspace, which holds the public id of the account. That id is not a secret. It is shown by get_connection_status, by list_quote_engines and on the Quotes tab of the dashboard.

{ "grietens_engine": "2", "name": "Kalns trusses", "products": ["roof_trusses"],
  "workspace": "gw-abcdefghijklmnop", "public_key": "<the public key of the engine>" }

Grietens then reads the card, checks that it names this account, proves the key with the same hello as a join and adds the engine to the account. The profile the account already has stays as it is. A card that names an account never joins anonymously as a new shop, because anyone can serve any card. Without the field an engine joins as its own new shop, as described above. Either way there is only one way to connect an engine, by its address and its own key, and a shop never ends up with two accounts.

Sources

All guides