[SOLVED] Retrieving data in CSV


#1

Hi there!

Can I retrieve data from my variables in text or a file format like CSV. Thanks for your help and great job!


#2

Yes, you can get it in two different ways:

1. Through our portal:
In the view of a variable, click on “Export to CSV”; you’ll get a CSV file with the last values of the variable:

2. Through our API

Just add the URL parameter ?format=csv when making a GET request for data. This will switch from a JSON response to a CSV response.

For example, let’s retrieve the last values of the variable “51f87c14f91b281ecfea6c8a”:

JSON format:

GET things.ubidots.com/api/v1.6/variables/51f87c14f91b281ecfea6c8a/values/?token=JmY7dCjdDx2nLAFYAvi3h1cUNyvV

CSV format:

GET things.ubidots.com/api/v1.6/variables/51f87c14f91b281ecfea6c8a/values/?token=JmY7dCjdDx2nLAFYAvi3h1cUNyvV&format=csv


#3

Btw

All the API endpoints return by default just 30 items. The page_size parameter can help you specify how many items to retrieve:

GET http://things.ubidots.com/api/v1.6/variables/53e262127625421973e7cdb5/values?token=33LqbmTXCqbxTMjW1fz5qKDJ93t98b&page_size=2000&format=csv

Other parameters you might want to check are the start and end parameters, so you can set the time-frame for which you’d like to get your data. For the full API endpoint documentation please visit: http://ubidots.com/docs/api/v1_6/data/get_variables_id_values.html


#4

How about get csv with python?


#5

You can use the requests python library and the API endpoint url:

http://docs.python-requests.org/en/master/

Regards


#6

Hi @yoelblack,

An example with the library requests

import requests
import csv

req = requests.get('http://things.ubidots.com/api/v1.6/variables/VARIABLE_ID/values?token=YOUR_TOKEN&page_size=2000&format=csv')

content_utf = req.content.decode('utf-8')
csv_reader = csv.reader(content_utf.splitlines(), delimiter=',')
with open('file.csv', 'wb') as csvFile:
    mylist = list(csv_reader)
    csv_writer = csv.writer(csvFile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
    for row in mylist:
        csv_writer.writerow(row)
    csvFile.close()

kath :slight_smile:


#7

I will try
Thanks


#8

Is it possible to download CSV without a token?

Or alternately, is it possible to create a “read-only” token (that can be used to retrieve/view data, but not upload/modify data?)

Thanks,

Jeff


#9

Dear Jeff, it is possible to create tokens for a group of devices, with read and/or write permissions. This is available in our business license. If you’re a business customer feel free to drop us a note in our support channel.

Here’s more info about this feature.