> For the complete documentation index, see [llms.txt](https://docs.dataclearinghouse.org/dch-2.0-documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.dataclearinghouse.org/dch-2.0-documentation/walkthroughs/create-update-and-publish-models-with-api.md).

# Create, update and publish models with API

#### Creating a Site via API <a href="#walkthrough-creatingandpublishingsiteandbuildingsemanticmodels-creatingasiteviamasonapi" id="walkthrough-creatingandpublishingsiteandbuildingsemanticmodels-creatingasiteviamasonapi"></a>

This request creates the Site resource and sets its metadata.

```bash
curl --location --request PUT 'https://dataclearinghouse.org/api/mv/v1/org/demo_org/site/demo_site' \
--header 'Content-Type: application/json' \
--header 'x-api-key: myApiKeyGoesHere' \
--data '{
    "orgId": "demo_org",
    "siteId": "demo_site",
    "label": "demo_site",
    "description": "Represents demo_site site in org demo_org.",
    "addressMeta": {
        "subAddress": null,
        "streetNumber": "3",
        "streetName": "Castray Esplanade",
        "subLocality": null,
        "locality": "Battery Point",
        "subAdministrativeArea": null,
        "administrativeArea": "Tasmania",
        "countryCode": "AUS",
        "postcode": "7004",
        "propertyId": null,
        "cadastralId": null,
        "titleId": null
    },
    "geolocation2d": {
        "latitude":-42.8867605,
        "longitude":147.338252
    }
}
```

#### Creating a Building via API <a href="#walkthrough-creatingandpublishingsiteandbuildingsemanticmodels-creatingabuildingviamasonapi" id="walkthrough-creatingandpublishingsiteandbuildingsemanticmodels-creatingabuildingviamasonapi"></a>

This request creates the Building resource and sets its metadata.

{% code fullWidth="true" %}

```bash
curl --location --request PUT 'https://dataclearinghouse.org/api/mv/v1/org/demo_org/site/demo_site/building/demo_building1' \
--header 'Content-Type: application/json' \
--header 'x-api-key: myApiKeyGoesHere' \
--data '{"orgId":"demo_org","siteId":"demo_site","buildingId":"demo_building1","label":"demo_building1","description":"demo_site demo_building1"}'
```

{% endcode %}

#### Updating Site RDF via API <a href="#walkthrough-creatingandpublishingsiteandbuildingsemanticmodels-updatingsiterdfviamasonapi" id="walkthrough-creatingandpublishingsiteandbuildingsemanticmodels-updatingsiterdfviamasonapi"></a>

This request overwrites the Site's draft RDF model. (It has no effect on the published model.)

```bash
curl --location --request PUT 'https://dataclearinghouse.org/api/mv/v1/org/demo_org/site/demo_site/draft' \
--header 'x-api-key: myApiKeyGoesHere' \
--data 'PREFIX brick:  <https://brickschema.org/schema/Brick#>
PREFIX unit:   <http://qudt.org/vocab/unit/>  
PREFIX senaps: <http://senaps.io/schema/1.0/senaps#> 
PREFIX owl:    <http://www.w3.org/2002/07/owl#>
PREFIX qudt:   <http://qudt.org/schema/qudt/>
PREFIX rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs:   <http://www.w3.org/2000/01/rdf-schema#>
 
PREFIX site: <dch:org/demo_org/site/demo_site#>
PREFIX b1:   <dch:org/demo_org/site/demo_site/building/demo_building1#> 
PREFIX dp1:  <dch:org/demo_org/demo_datapool1#> 
  
site: a brick:Site ;
    brick:isMeteredBy site:siteMeter .
 
site:siteMeter a brick:Electrical_Meter ;
    brick:hasSubMeter b1:submeter1 ;
    brick:hasPoint dp1:site_energy . '
```

#### Updating Building RDF via API <a href="#walkthrough-creatingandpublishingsiteandbuildingsemanticmodels-updatingbuildingrdfviamasonapi" id="walkthrough-creatingandpublishingsiteandbuildingsemanticmodels-updatingbuildingrdfviamasonapi"></a>

This request overwrites the Building's draft RDF model. (It has no effect on the published model.)

```bash
curl --location --request PUT 'https://dataclearinghouse.org/api/mv/v1/org/demo_org/site/demo_site/building/demo_building1/draft' \
--header 'Content-Type: text/plain' \
--header 'x-api-key: myApiKeyGoesHere' \
--data 'PREFIX  brick:  <https://brickschema.org/schema/Brick#>
PREFIX unit:   <http://qudt.org/vocab/unit/>  
PREFIX senaps: <http://senaps.io/schema/1.0/senaps#> 
PREFIX owl:    <http://www.w3.org/2002/07/owl#>
PREFIX qudt:   <http://qudt.org/schema/qudt/>
PREFIX rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs:   <http://www.w3.org/2000/01/rdf-schema#>
 
PREFIX site: <dch:org/demo_org/site/demo_site#>
PREFIX b1:   <dch:org/demo_org/site/demo_site/building/demo_building1#> 
PREFIX dp1:  <dch:org/demo_org/demo_datapool1#>  
 
b1: a brick:Building ;
    brick:isMeteredBy b1:meter1 .
 
b1:meter1 a brick:Electrical_Meter ;
    brick:isSubMeterOf b1:siteMeter
    brick:hasPoint dp1:b1_energy . 
```

#### Publish a Site via API <a href="#walkthrough-creatingandpublishingsiteandbuildingsemanticmodels-publishasiteviamasonapi" id="walkthrough-creatingandpublishingsiteandbuildingsemanticmodels-publishasiteviamasonapi"></a>

This request causes a sequence of events to occur across semantic models in the site. These are:&#x20;

* dependency discovery
* intersection of site/building models with datapool models
* root node composition
* inferencing
* validation
* merging&#x20;
* updating represented resources to point to the newly merged models

The result of this is that the site and its buildings are then available to be queried through the query API, and returned solutions will reflect the newer version of the semantic model.

```bash
curl --location --request POST 'https://dataclearinghouse.org/api/mv/v1/org/demo_org/site/demo_site/publish' --header 'x-api-key: myApiKeyGoesHere'
```
