Define/update formulas via API

Hello,

Since for some integration, I need to keep track of which fields are filled in and which are left blank for each record, I wrote a simple script to generate a Baserow formula to put as auxiliary field NON-EMPTY-FIELDS. For example, if a table has fields ‘Full_Name’, ‘Notes’ and ‘Active’, then the generated formula would be

concat(if(isblank(field(‘Full_Name’)),’’,‘Full_Name ‘),if(isblank(field(‘Notes’)),’’,‘Notes ‘),if(isblank(field(‘Active’)),’’,'Active '))

Inserting the formula from the interface works fine, but it would be more practical to be able to do that via the API. According to the open API documentation it should be possible to make both POST and PATCH request to create and update the formula respectively. However, in both cases I get a 415 Unsupported Media Type error. The body of the request is

{“formula”:“concat(if(isblank(field(‘Full_Name’)),’’,‘Full_Name ‘),if(isblank(field(‘Notes’)),’’,‘Notes ‘),if(isblank(field(‘Active’)),’’,'Active '))”,“formula_type”:“text”,“name”:“NON_EMPTY_FIELDS”,“type”:“formula”}

Is there a way I can format this formula to make the request successful?

Hey @maurizio.moreschi, what you are trying to do is 100% possible and supported. That error indicates you need to set the request header "Content-Type: “application/json”.

If that doesn’t work could you provide the request you are sending with headers and body and/or the code doing it? Make sure to anonymize any tokens, passwords, data etc

1 Like

Yes, that was indeed my first thought, but when I added the content type to the header, the authorization stopped working and I got a 401. Below here the piece of my code that makes the POST request (the Content-Type row is currently commented out). The code is in Go as that is our primary back-end language, I hope that’s not a problem.

	preq, _ := http.NewRequest("POST", "https://api.baserow.io/api/database/fields/table/"+tableID+"/", bytes.NewBuffer(json_data))
	preq.Header = http.Header{
		"Authorization": []string{"Token "+APIkey},
//		"Content-Type": []string{"application/json"},	
	}
	pres, _ := client.Do(preq)

Ah I’ve just realized the problem. The Token .... based authentication is only supported for endpoints shown in the API docs for your specific database (you can see this by clicking on your database in the side bar and selecting view api docs). All other endpoints only support authentication using a JWT token including the field update endpoint.

We plan to extend support for api tokens to more endpoints, however for now you can get and refresh JWT tokens for a user using the endpoints described here Baserow API spec. Once you have a JWT token you can instead set the Auth header to JWT ......

Sorry for any inconvenience this causes, we really want to expand the API token functionality asap!

1 Like