Changing models
This page is about changing a project’s models; Reading models is about reading them.
A program can change a project’s models as a person can in the app: make packages, elements and relationships, set their values, attach files, rename and move them, and delete them, one at a time or up to 200 in one call. Every change goes through the same rules as the app’s, and is signed with the key’s name (API · <key name>) wherever the app shows who changed something.
Changing needs a key with the contributor or admin role for the project (API keys). Metamodels can’t be changed through the API: they’re read only (Metamodels).
| Call | Does |
|---|---|
POST /v1/projects/{projectId}/packages |
Makes a package inside a model |
GET /v1/packages/{packageId} |
Gives one package, with its ETag |
PATCH /v1/packages/{packageId} |
Renames it, changes its notes, or moves it |
DELETE /v1/packages/{packageId} |
Deletes it, with everything in it |
POST /v1/projects/{projectId}/elements |
Makes an element, with its values |
PATCH /v1/elements/{elementId} |
Changes its name, notes or values, or moves it |
DELETE /v1/elements/{elementId} |
Deletes it, with its relationships and diagram shapes |
POST /v1/projects/{projectId}/relationships |
Makes a relationship between two elements |
PATCH /v1/relationships/{relationshipId} |
Changes its name, notes, values or ends |
DELETE /v1/relationships/{relationshipId} |
Deletes it |
PUT /v1/elements/{elementId}/attachment |
Attaches a file to an element, replacing any it had |
DELETE /v1/elements/{elementId}/attachment |
Removes an element’s attached file |
POST /v1/projects/{projectId}/batch |
Makes up to 200 of these changes at once, all or none |
To notice what others deleted, see Keeping another system in step.
Making an element
Section titled “Making an element”curl -X POST https://api.farkitect.com/v1/projects/$PROJECT/elements \ -H "Authorization: Bearer $FARKITECT_API_KEY" \ -H "Content-Type: application/json" \ -H "Idempotency-Key: $(uuidgen)" \ -d '{ "type": "M2 ArchiMate 4/Application Component", "name": "Customer Portal", "packageId": "m88…", "notes": "Serves **retail** customers.", "properties": { "Annual Cost": 125000, "Lifecycle": "Production", "Go Live": "2025-03-01" } }'The answer is 201 with the element as GET /v1/elements/{elementId} gives it, its address in Location and its version in ETag.
typeis the element type’s id, or its name as"Metamodel/Type name", or a type name alone when only one metamodel the model imports has a type of that name.packageIdis where to put it. For an element that belongs to another, such as an attribute, giveownerIdinstead: it’s kept in its owner’s package.propertiesare set by property type name (or id), each in its natural type, as values are read (Reading models): a number,trueorfalse,YYYY-MM-DDfor a date, an enumeration value’s name.nullclears a value. Property types not given keep their type’s default.
The element must be one the model can hold, as in the app. It’s refused, with the reason, when its type is:
- abstract (it can’t have elements of its own) or retired;
- a relationship type;
- from a metamodel the model doesn’t import (add the import in the app, in the model’s Properties);
- a subordinate type (such as an attribute) without an owner whose type has it.
A value that doesn’t fit its property type is refused too. Nothing is made when anything is refused: the answer is 422 validation_failed, and its errors name each field.
"errors": [ { "field": "body.properties.Annual Cost", "message": "must be a number" }, { "field": "body.properties.Lifecycle", "message": "must be one of \"Planned\", \"Production\", \"Retired\"" }]Changing and moving
Section titled “Changing and moving”PATCH changes only what’s sent. In properties, only the values sent change; null clears one.
{ "name": "Customer Portal (retail)", "properties": { "Annual Cost": 130000, "Owner": null } }To move an element, give packageId (another package) or ownerId (to put it under another element; null takes it out from under its owner, into the same package). A package moves with parentId. Moves follow the app’s rules: nothing moves into itself, and a relationship whose ends both leave the model it’s kept in moves with its source.
Relationships
Section titled “Relationships”POST /v1/projects/{projectId}/relationships{ "type": "M2 ArchiMate 4/Serving", "sourceId": "j57…", "targetId": "k12…", "properties": { "Criticality": "High" } }type is given as for an element. Left unnamed, a relationship is named from its ends and its type’s verb, as in the app (“Customer Portal serves Retail Banking”). It’s kept in the source’s package unless packageId names another package of the source’s model. Its ends may be in two models: the type must then be from a metamodel that both models import, and each end in what the API returns says which model it is in (source.model, target.model).
It’s refused, with the reason, when its type can’t join that pair, by the rules the app follows when you connect two shapes:
- a standard metamodel’s relationship type joins only that metamodel’s elements; a common metamodel’s (such as M2 Common) joins elements of the metamodels the model imports;
- a type with valid pairs joins only those pairs of element types. A subtype counts as its supertype, so a pair for Party also allows a Customer that is a kind of Party.
PATCH with sourceId or targetId joins it to another element, by the same rules. A name made from its ends follows the new ends; a name someone chose is kept.
Many changes at once
Section titled “Many changes at once”POST /v1/projects/{projectId}/batch makes up to 200 changes in one call, in order, all or none. Each operation names what to do (op: create, update or delete), to what (kind: package, element or relationship), and the body the single call takes. An update or delete names the item’s id, and may give its ifMatch.
A create may have a ref, a name starting with $, which stands for its new id in later operations. So elements and the relationships between them are made in one call:
{ "operations": [ { "op": "create", "kind": "element", "ref": "$portal", "body": { "type": "Application Component", "name": "Customer Portal", "packageId": "m88…" } }, { "op": "create", "kind": "element", "ref": "$crm", "body": { "type": "Application Component", "name": "CRM", "packageId": "m88…" } }, { "op": "create", "kind": "relationship", "body": { "type": "Flow", "sourceId": "$crm", "targetId": "$portal" } }, { "op": "update", "kind": "element", "id": "q41…", "body": { "properties": { "Lifecycle": "Retired" } } } ]}The answer lists each operation’s result in order: its id, its ref, and the item as it is now (null for a delete). If any operation is refused, nothing is changed: the problem’s detail names the operation (“Operation 2 (create relationship): …”) and its errors name the fields, such as body.operations[2].body.type. Every item must be in the batch’s project. A batch counts as one change against the per-minute limit, and its Idempotency-Key covers the whole batch.
Attaching files
Section titled “Attaching files”PUT /v1/elements/{elementId}/attachment attaches a file to an element: the file itself is the body, its type is in Content-Type, and its name in Content-Disposition. It replaces any file the element had. The answer is the element as it is now, with the attachment’s name, type and size, and its new ETag.
curl -X PUT https://api.farkitect.com/v1/elements/ELEMENT_ID/attachment \ -H "Authorization: Bearer $FARKITECT_API_KEY" \ -H "Content-Type: application/pdf" \ -H 'Content-Disposition: attachment; filename="Service design.pdf"' \ --data-binary @"Service design.pdf"As in the app, only an element whose type allows an attachment takes one, and a type whose attachments are images takes images only; anything else is refused with 422 saying why. A file can be at most 4 MB through the API (413 too_large otherwise); larger files can be attached in the app. Without Content-Disposition the file is called attachment; for a name with characters other than plain letters, send filename*=UTF-8'' followed by the name, percent-encoded.
DELETE /v1/elements/{elementId}/attachment removes the file; an element with none is left as it is. Both take If-Match and Idempotency-Key, as every change does. Reading a file back is GET /v1/elements/{elementId}/attachment.
Deleting
Section titled “Deleting”DELETE takes what deleting in the app takes: a package with everything in it, an element with the elements it owns, its relationships and its shapes on diagrams, and a relationship with its connectors on diagrams. The answer is 204.
A delete that would take more than 200 elements and relationships runs in the background instead: the answer is 202 with a job, and its address in Location. Ask GET /v1/jobs/{jobId} every few seconds until its status is done; deleted then says how many elements and relationships it deleted. It’s the app’s own delete, so it takes the same things with it. An organisation runs one job at a time, exports included (Exports). In a batch, a delete must be small: a larger one is refused with 413 too_large.
A whole model (a top-level package) is made and deleted in the app.
Retrying safely: Idempotency-Key
Section titled “Retrying safely: Idempotency-Key”A request can time out after Farkitect made the change. Sent again, it would make it twice. To retry safely, send an Idempotency-Key header, any unique string (a UUID is usual), with every change:
- the same request with the same key, within a day, gets the first answer again, and nothing is changed a second time;
- the same key with a different request is refused with
422idempotency_key_reused; - a request that was refused stores nothing, so it can be put right and sent again with the same key.
Not overwriting others: If-Match
Section titled “Not overwriting others: If-Match”Someone may change an item between your program reading it and changing it. To change it only if nobody has, send its ETag back in If-Match:
- Read it:
GET /v1/elements/{elementId}(or/v1/packages/{packageId}) answers with anETagheader. Every change’s answer has one too. - Send the change with
If-Match: <that ETag>, exactly as it came. - If the item changed in between, nothing changes and the answer is
412precondition_failed. Read it again, apply your change to what’s there now, and send it with the newETag.
Without If-Match, a change is made whatever happened since. Both headers are optional, but a program that changes models for others should use both.
Limits
Section titled “Limits”Each change counts against the key’s changes per minute, as well as its requests (Rate limits); a batch counts as one. To change many items, send them in batches, one batch after another: it’s quicker for your program, uses fewer of its calls, and is kinder to the people who have the project open, whose app takes in each change as it happens (a batch arrives as one change).