[SOLVED] DataSourceName support for HTTP

Hi,

afaik TCP API “translate.ubidots.com” supports the possibility to set the DataSourceName parameter to create a Device on demand.

Since I can’t use TCP but HTTP, is it possible to do the same via a HTTP header or something else?
thank you!
Sam

Hi @universam,

You can currently create a Data Source using our Devices API endpoint. This endpoint looks like this:

https://things.ubidots.com/api/v1.6/devices/yourDsLabel

Where yourDsLabel is the label of your actual Data Source; if you don’t have a Data Source with that label, it will create that Data Source.

If you want to change the name of your Data Source, then you should do a PATCH request with the new name.

Let me give you an example using cURL:

curl -X POST -H "X-Auth-Token: yourToken" http://things.ubidots.com/api/v1.6/devices/yourDsLabel?status=true

This will respond something like this:

{"_status": {"_datasource": {"new": true, "id": idOfYourDataSource}}}

You should get the id value from within the _datasource property and then do the PATCH request:

curl -X PATCH -H "X-Auth-Token: yourToken" -H "Content-Type: application/json" http://things.ubidots.com/api/v1.6/datasources/yourDsId -d '{"name": "New Name"}' 

Hope this helps
@kath

Thank you very much @kath,

this is a very helpful tutorial! Exactly what I was hoping to!

Hence my question is still would it be possible to create variables the same way like the Data Source over this API?

This would be awesome!

Thanks,
Sam

Yes it is possible,

You just have to send the variable label you want to create in the request body, like this:

curl -X POST -H "X-Auth-Token: yourToken" -H "Content-Type: application/json" -d '{"variableLabel": 0}' "http://things.ubidots.com/api/v1.6/devices/yourDsLabel/?status=true"

The conditions that @kath explained before will be exactly the same in this one. If you have a Variable created with that label it will use that Variable, but if a Variable with that label doesn’t exist inside your Data Source it will create a new one.

Hope this helps :blush:

Regards!

@juanda95
Thats an awesome explanation, I’ve tried it and it works really great!

This gives a lot more freedom by creating devices and variables on the fly!

Thank you @kath @juanda95 so much!

Sam

1 Like