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!!
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)
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
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!
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.