Answered

API - How can we update only specific field of a form?


  • Anonymous
  • 0 replies

Hello,

We are using Typeform API to update form’s redirect url; however, when we do that, fields of form are deleted.

Endpoint (PUT) : https://api.typeform.com/forms/:form_id

Sample Request:

{"title": "Shipping Calculator","settings": {"redirect_after_submit_url" : "https://google.com"}}

When we, do that; all fields of form have been deleted. 

Is there any way to update only specific field (setting) of form?

Thanks.

icon

Best answer by picsoung 14 April 2021, 02:48

View original

11 replies

Userlevel 7
Badge +5

Hi @ukadakal this is a great question, and thanks for stopping by! When using the PATCH method, you’ll need to send the entire definition of the form, including the changes you want made. It isn’t possible to only update one field at the moment. :( 

Userlevel 7
Badge +5

Hi @ukadakal 

As ​​​@Liz explained, the current PATCH method for partial update only works on certain fields and has it’s own syntax.

Unfortunately the redirect_after_submit_url option is not working fields for this PATCH method.

You should instead use the PUT method to update the form.You would need to pass the whole new form definition with including the fields and other properties.

Thank you @Liz and @picsoung. I have a follow up question. We are building an integration with Typeform. When user submits a form, we send selected answers to an external API. We pass the information we receive from this external API along with information from the first form to a second form (using redirect_after_submit_url) as hidden fields. But we also want to be able to display the information from the external API to the user in the second form. Is there a way to achieve this? We cannot seem to find a way display it because they come as hidden fields. 

Userlevel 7
Badge +6

@ukadakal - just a thought: have you set up the hidden fields (from the external set) as ‘variables’ in the second form? There needs to be a receiver there in order to capture the data from the string being passed into the form in order to display. (at least that’s my paraphrasing of it)

 

des

@john.desborough that did the trick. Thank you!

Userlevel 7
Badge +6

@john.desborough that did the trick. Thank you!

@ukadakal - had that same problem on my form as well the first time i tried it.. (what happens in Community, stays in Community lol)

des

Could I please get a coded example of a patch request for a valid field value such as the form title?

This is how I am trying to rename the form title (for Google Apps Script):

  var body = {'op' : 'replace',
'path': '/title',
'value': "succesfully_renamed"}

var options = {
"method" : "patch",
"payload" : body,
"headers": {'Authorization': {API_key},
}
}

var url = 'https://api.typeform.com/forms/'+ {form_id}


request = UrlFetchApp.fetch(url, options)

I get the following error when executing the code

Error	
Exception: Request failed for https://api.typeform.com returned code 403. Truncated server response: {"code":"INSUFFICIENT_PERMISSIONS","description":"not enough permissions to complete the action"} (use muteHttpExceptions option to examine full response)
renameSurvey @ TypeFormAPI.gs:370

 

But the following get request is successful:


var options = {
"headers": {'Authorization': {API_key} }
}

var url = 'https://api.typeform.com/forms/'+ {form_id}


request = UrlFetchApp.fetch(url, options)

Thank you in advance!

Userlevel 7
Badge +5

@mathio do you happen to have an example of the above? 

Userlevel 7
Badge +5

Hi @userresearcher 

I tried recreating the request in JavaScript.

  1. Does your API key start with tfp_ and does it have correct permissions?
  2. When you are sending Authorization header, you should send the value as Bearer <key>, not just the key itself.
  3. When I send the body from your post, I get this error:
    {
    code: 'VALIDATION_ERROR',
    description: 'The payload is invalid.',
    details: [
    { code: 'WRONG_TYPE', description: 'should be array', in: 'body' }
    ]
    }

     

  4. So I looked at the docs and we need to post the body as array:
    var body = [
    {
    'op' : 'replace',
    'path': '/title',
    'value': "I am your new form name"
    }
    ]

My final code in JavaScript that worked for me looks like this:

import fetch from 'node-fetch';

const API_KEY='tfp_***'

const id = '<form-id>'

const url = 'https://api.typeform.com/forms/' + id

const body = [
{
'op' : 'replace',
'path': '/title',
'value': "I am your new form name"
}
]

const options = {
"method" : "patch",
"body" : JSON.stringify(body),
"headers": {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json'
},
}

fetch(url, options).then(res => console.log('status', res.status)) // should be 'status 204'

 

Hi, thank you for the quick reply 

I was indeed writing Bearer just before the key, but I generated a new token with all permissions -and changed the body format- and it works fine now, so that was the main problem.

Thank you so much again for the thorough explanation!

Userlevel 7
Badge +5

Glad to hear it’s working, @userresearcher ! 

Reply