Creating ledger instances with curl

I am trying to create an instance using curl and am unable to. I am able to create a ledger with curl but when I try to create an instance i get a not found error. I am using the command below:
curl -v -X POST https://api.particle.io/v1/products/:PRODUCT-ID/ledgers/:LEDGER-NAME/instances
-H "Authorization: Bearer “:ACCESS-TOKEN”
-H "Content-Type: application/json"
-d '{
"instance": {
"scope": {
"type": "Device",
"value": “:DEVICE”-ID
},
"data": {
"freq1": 600,
"freq2": 900
}
}
}'

I believe you need to use PUT not POST to set ledger instance values.

I am still getting the same error when i use PUT to try to create an instance. {"ok":false,"error":"Not Found"}

Maybe we are not allowed to create a instance with curl? I thought since we were able to create a ledger with curl we would be able to create an instance. I had also read that when the device interacts with the ledger an instance would automatically be created. It is a cloud to device ledger by the way.

There is a possibility that a cloud-to-device ledger must be created in sandbox or organization, not in a product. I'm only 60 % sure that's true. [Edit: not true, see below.]

You should be able to create a cloud-to-device instance because it would make sense to pre-configure settings to be pushed to a device before it first connects.

I can can only create it manually on the particle console.

The console just uses the Particle cloud API, so it implies that it must be possible, but it will take a little while to find out what it's actually doing.

I set a value from a product-scoped cloud-to-device ledger from the console and monitored it in the browser debug log. It puts the Device ID in the URL, not in the body. That's probably how you have to do it.

This is the URL requested:

/v1/products/:product_id/ledgers/:ledger_name/instances/:device_id?set_mode=replace

I was able to get it to work with the org endpoint

curl -X PUT "https://api.particle.io/v1/orgs/:org/ledgers/:ledger_name/instances/:device_id" \
     -H "Authorization: Bearer :token" \
     -H "Content-Type: application/json" \
     -d '{
           "instance": {
             "scope": {
               "type": "Device",
               "value": ":device_id"
             },
             "name": ":ledger_name",
             "size_bytes": 1234,
             "data": {
               "freq1": "600",
               "freq2": "900"
             }
           }
         }'
{"instance":{"version":"0b6c042f-0815-491a-b709-66e49dcc4856","name":":ledger_name","scope":{"type":"Device","value":":device_id","not_owned":true},"size_bytes":29,"data":{"freq1":"600","freq2":"900"},"created_at":"2024-10-22T18:48:57.729Z","updated_at":"2024-10-22T18:48:57.729Z"}}
1 Like

Thanks I fought with this for hours on Saturday. I was doing this on my own personal account before trying it on the organization. It worked with products/:PRODUCT-ID.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.