Home > OS >  Create multiple twins in Azure Digital Twin via API
Create multiple twins in Azure Digital Twin via API

Time:12-16

I am trying to use the Azure Digital Twins API to create twins of the model I have uploaded. So far, I can successfully create one twin per model per API request.

Is there any way to create multiple twins from multiple models in 1 request?

So far I have tried parsing various iterations of the following as the JSON payload.

{"digitalTwinsGraph": {
        "digitalTwins": [
            {
                "$dtId": "Building1",
                "$etag": "W/\"28bcd6a5-f0ab-477c-b314-a11193d2bf61\"",
                "address": {
                    "$metadata": {}
                },
                "area": {
                    "$metadata": {}
                },
                "capacity": {
                    "$metadata": {}
                },
                "occupancy": {
                    "$metadata": {}
                },
                "temperature": {
                    "$metadata": {}
                },
                "humidity": {
                    "$metadata": {}
                },
                "CO2": {
                    "$metadata": {}
                },
                "$metadata": {
                    "$model": "dtmi:digitaltwins:rec_3_3:core:Building;1"
                }
            },
            {
                "$dtId": "Floor4",
                "$etag": "W/\"c34f7f70-2511-4299-bb10-0b5a17ee39cd\"",
                "area": {
                    "$metadata": {}
                },
                "capacity": {
                    "$metadata": {}
                },
                "occupancy": {
                    "$metadata": {}
                },
                "temperature": {
                    "$metadata": {}
                },
                "humidity": {
                    "$metadata": {}
                },
                "CO2": {
                    "$metadata": {}
                },
                "$metadata": {
                    "$model": "dtmi:digitaltwins:rec_3_3:core:Level;1"
                }
            },
            {
                "$dtId": "Office",
                "$etag": "W/\"ce195644-3174-4a61-ac37-00d8a0f95713\"",
                "area": {
                    "$metadata": {}
                },
                "capacity": {
                    "$metadata": {}
                },
                "occupancy": {
                    "$metadata": {}
                },
                "temperature": {
                    "$metadata": {}
                },
                "humidity": {
                    "$metadata": {}
                },
                "CO2": {
                    "$metadata": {}
                },
                "$metadata": {
                    "$model": "dtmi:digitaltwins:rec_3_3:core:Room;1"
                }
            },
            {
                "$dtId": "MainWorkingSpace",
                "$etag": "W/\"507e727c-6a10-45c2-8293-84817e37981b\"",
                "area": {
                    "$metadata": {}
                },
                "capacity": {
                    "$metadata": {}
                },
                "occupancy": {
                    "$metadata": {}
                },
                "temperature": {
                    "$metadata": {}
                },
                "humidity": {
                    "$metadata": {}
                },
                "CO2": {
                    "$metadata": {}
                },
                "$metadata": {
                    "$model": "dtmi:digitaltwins:rec_3_3:core:Zone;1"
                }
            },
            {
                "$dtId": "SmallMeetingRoom",
                "$etag": "W/\"ac3ed267-0320-46dc-9247-ed93ef81bfcd\"",
                "area": {
                    "$metadata": {}
                },
                "capacity": {
                    "$metadata": {}
                },
                "occupancy": {
                    "$metadata": {}
                },
                "temperature": {
                    "$metadata": {}
                },
                "humidity": {
                    "$metadata": {}
                },
                "CO2": {
                    "$metadata": {}
                },
                "$metadata": {
                    "$model": "dtmi:digitaltwins:rec_3_3:core:Zone;1"
                }
            },
            {
                "$dtId": "BigMeetingRoom",
                "$etag": "W/\"fb77e308-0a30-4a46-a7a3-c7bf7306af38\"",
                "area": {
                    "$metadata": {}
                },
                "capacity": {
                    "$metadata": {}
                },
                "occupancy": {
                    "$metadata": {}
                },
                "temperature": {
                    "$metadata": {}
                },
                "humidity": {
                    "$metadata": {}
                },
                "CO2": {
                    "$metadata": {}
                },
                "$metadata": {
                    "$model": "dtmi:digitaltwins:rec_3_3:core:Zone;1"
                }
            },
            {
                "$dtId": "Ground",
                "$etag": "W/\"4a0f4290-9eab-4985-9c13-51ecb2f6e7ae\"",
                "area": {
                    "$metadata": {}
                },
                "capacity": {
                    "$metadata": {}
                },
                "occupancy": {
                    "$metadata": {}
                },
                "temperature": {
                    "$metadata": {}
                },
                "humidity": {
                    "$metadata": {}
                },
                "CO2": {
                    "$metadata": {}
                },
                "$metadata": {
                    "$model": "dtmi:digitaltwins:rec_3_3:core:Level;1"
                }
            },
            {
                "$dtId": "Floor1",
                "$etag": "W/\"3afb4ff2-22ea-4b1f-b7a7-109f2b3b95f4\"",
                "area": {
                    "$metadata": {}
                },
                "capacity": {
                    "$metadata": {}
                },
                "occupancy": {
                    "$metadata": {}
                },
                "temperature": {
                    "$metadata": {}
                },
                "humidity": {
                    "$metadata": {}
                },
                "CO2": {
                    "$metadata": {}
                },
                "$metadata": {
                    "$model": "dtmi:digitaltwins:rec_3_3:core:Level;1"
                }
            },
            {
                "$dtId": "Floor2",
                "$etag": "W/\"b630afa3-c90d-4889-95d8-a214c6bbbe78\"",
                "area": {
                    "$metadata": {}
                },
                "capacity": {
                    "$metadata": {}
                },
                "occupancy": {
                    "$metadata": {}
                },
                "temperature": {
                    "$metadata": {}
                },
                "humidity": {
                    "$metadata": {}
                },
                "CO2": {
                    "$metadata": {}
                },
                "$metadata": {
                    "$model": "dtmi:digitaltwins:rec_3_3:core:Level;1"
                }
            },
            {
                "$dtId": "Floor3",
                "$etag": "W/\"de51bc09-2961-4500-8289-a3e203763073\"",
                "area": {
                    "$metadata": {}
                },
                "capacity": {
                    "$metadata": {}
                },
                "occupancy": {
                    "$metadata": {}
                },
                "temperature": {
                    "$metadata": {}
                },
                "humidity": {
                    "$metadata": {}
                },
                "CO2": {
                    "$metadata": {}
                },
                "$metadata": {
                    "$model": "dtmi:digitaltwins:rec_3_3:core:Level;1"
                }
            },
            {
                "$dtId": "Floor5",
                "$etag": "W/\"d22277ed-7a41-4f24-8b40-e8dbdd9aeaa9\"",
                "area": {
                    "$metadata": {}
                },
                "capacity": {
                    "$metadata": {}
                },
                "occupancy": {
                    "$metadata": {}
                },
                "temperature": {
                    "$metadata": {}
                },
                "humidity": {
                    "$metadata": {}
                },
                "CO2": {
                    "$metadata": {}
                },
                "$metadata": {
                    "$model": "dtmi:digitaltwins:rec_3_3:core:Level;1"
                }
            },
            {
                "$dtId": "Floor6",
                "$etag": "W/\"323bce27-ad21-4e17-9754-02c5c59065a0\"",
                "area": {
                    "$metadata": {}
                },
                "capacity": {
                    "$metadata": {}
                },
                "occupancy": {
                    "$metadata": {}
                },
                "temperature": {
                    "$metadata": {}
                },
                "humidity": {
                    "$metadata": {}
                },
                "CO2": {
                    "$metadata": {}
                },
                "$metadata": {
                    "$model": "dtmi:digitaltwins:rec_3_3:core:Level;1"
                }
            },
            {
                "$dtId": "967a6e7b-9590-474f-bd4d-be5fa1e1e2c5",
                "$etag": "W/\"71c04c7f-9394-4e41-8e1e-f0f886b36fb6\"",
                "area": {
                    "$metadata": {}
                },
                "capacity": {
                    "$metadata": {}
                },
                "occupancy": {
                    "$metadata": {}
                },
                "temperature": {
                    "$metadata": {}
                },
                "humidity": {
                    "$metadata": {}
                },
                "CO2": {
                    "$metadata": {}
                },
                "$metadata": {
                    "$model": "dtmi:digitaltwins:rec_3_3:core:Level;1"
                }
            }}

I am getting the following errors:

{
    "errors": {
        "twinCreate": [
            "The twinCreate field is required."
        ],
        "digitalTwinsGraph.digitalTwins": [
            "Invalid property identifier character: {. Path 'digitalTwinsGraph.digitalTwins', line 306, position 12."
        ]
    },
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "detail": null,
    "instance": null,
    "extensions": {
        "traceId": "|bfeb7647-4044d66d0401388a."
    }
}

I cannot seem to find any docs explaining if this is possible or even what the twinCreate field is.

CodePudding user response:

Unfortunately, you can't do this in one API call. You can add multiple models at the same time, but you can't add multiple twins in one call.

It would make a good feature request.

  • Related