How can we send data from Python in tulip database table, please suggest

How can we send data from Python in tulip database table, please suggest.

hello @nitingoel82, welcome to the Tulip Community!!

that’s a great question, and can be achieved with the use of Tulip Tables API: How to Use the Table API | Tulip Help Center - Support for Building Manufacturing Apps. you will need to create an API key for your Tulip Table and then send the data using the /tables/{tableId}/records (POST) endpoint. you can navigate to https://{{instance_name}}.tulip.co/apiDocs for the API documentation.

this guide: Guide: Using the Table API and Python to import data from a Tulip Table into Power BI maybe useful (although it provides an example of getting data from a Tulip Table to PowerBI) it provides good library suggestions and reference material for how to use Python to interact with data in Tulip Tables.

let us know if you have further questions!!

1 Like

I have wrote program in Python and need to send data through python job, how to authenticate tulip and send data, article is not useful

OK @nitingoel82, have you created a Bot that allows for writing to a Tulip Table?? you can use that information in the Headers for Authentication:


ok, What information i need mention in python program

Hi @nitingoel82, can you describe a bit more what your Python script does? Is it generating the data you want to store in tables? I’ve included an example of how you might store one row of information for the script execution, but you could loop this if you wanted to quite easily.

import time
import requests
import json

header = #Add Auth Header from Tulip Bot
endpoint = #Add API endpoint to hit from API docs i.e. "https://<instance-name>.tulip.co/api/v3/tables/<tableID>/records"

# Form JSON object to send to endpoint, example below is the ID column
# and one faux column. This is getting two variables test1 and test2
json_dict = {'id': test1, 'abcde_column1': test2}
json_object = json.dumps(json_dict)

# Now we need to execute the POST request with the endpoint, header, and object.
response = requests.post(endpoint, headers = {'Authorization' : header}, data=json_object, verify=False)
print(response)

# If looping, I'd recommend the time sleep function to add a slight delay for responses
# time.sleep(0.2)
1 Like

I am getting response 422 error while insert the data.

Can you send me a link to the table you’re trying to POST to? My guess is the json_dict value was not updated appropriately for your request. Feel free to send me an email at grant.levy@tulip.co as well to share details around the JSON object you’re building.

json_dict = {‘id’: ‘test1’, ‘abcde_column1’: ‘test2’}

Hey @markwkiehl! Welcome to community,

Here is a basic gist that includes the kinda key functions I use when hitting the Tables API.

Please let me know if you have any errors pushing data into a table. In my experience, a 422 response code would indicate you are trying to create a record when a record with that id already exists.

Pete

I believe you can also print out the returned JSON in most cases and get a more descriptive error.
422’s can be a correctly formatted request, but something went wrong. You’re really lost in the woods if you’re getting a 500 :slight_smile:

get same errors 422,so what’s the correct format???

Hi nitingole:

first confirm your python code is correct.And I’m sure you know the post request.
r = requests.post(url, json=ss, auth=(API_KEY, SECRET), headers = dicheaders )
url is your api url.
ss is your post parameters, your can directly us DICT type, because the REQUEST support convert to json if you use JSON=xxx.
API_KEY and SECRET is setting in your BOT in tulip.
headers is interesting,maybe do not need it but you can also get it from BOT,it’s common.
That’s all for the post request.But you need notice as below:
1.please print (r.text) to see the detail error info,not the exception,it’s just useless.
2.if your process is reading from the CSV or excel then post,
maybe you use PANDAS or OPENPYXL,sometimes the data maybe in wrong format in your code.especially in double,float and some numbers.That will cause 422 error in your program.
3.Also you need notice BLANK in your file,please use BEJOSN to check your parameter format.

it almost costs me half a day to solve these,and good luck for you! :grinning:

1 Like

Hey @tony.wang -

A 422 response code indicates that the record already exists. To update records, you should use the PUT endpoint to update records.

They way I hit this in python is:

def updateTable(instance,table,data,key):
    ########################FUNCTION DESCRIPTION#######################
    #This updates a record in a given table
    ###################################################################
    url = 'https://{}/api/v3/tables/{}/records/{}'.format(instance,table,data['id'])
    payload = data


    headers = {"accept": "application/json", "Authorization": key,"Content-Type": "application/json"}
    r = requests.put(url, data=json.dumps(payload), headers=headers)
    if r.status_code not in [200,201]:
        raise Exception("Bad error code: {}".format(r.content))

Pete

No,422 is a parameter format error.
The API will define the parameter format it needs contain the type .
if the api need a para named XXX,and the type is INT,but you send {“xxx”:“123”},
then will cause 422 error ,because the “123” is not INT ,It’s string.
The only thing missed in tulip example is the exception text,the exception text will tell the customer which parameter is wrong and why it’s wrong .So just add one line in the example,something will be clear. :grinning: