The Google Forms API is currently in Restricted Beta. To use the API, apply for access at the Early Adopter Program page.

Update a form or quiz

To add content to a form or update the settings, metadata, or content, use the batchUpdate() method, which groups changes together in a batch so that if one request fails, none of the other (potentially dependent) changes are written.

The batchUpdate() method returns a response body, within which is a response for each request. Each response occupies the same index as the corresponding request; for requests with no applicable response, the response at that index will be empty.

Before you begin

Perform the following tasks before proceeding with the tasks on this page:

  • Complete authorization/authentication and credentials setup in the Early Adopter Program instructions

Update metadata, settings, or items

The following example shows how to update a form's metadata, but the structure is the same for content and settings—they use the updateItem or updateSettings requests instead of updateFormInfo. For each request, you supply the name of the field to be changed and the updated value, along with an updateMask value to limit changes to the fields you've specified.

REST

To update the form's description, call the batchUpdate() method with the form ID and the updated description value.

Sample request body

    "requests": [{
        "updateFormInfo": {
            "info": {
                "description": "Please complete this quiz based on this week's readings for class."
            },
            "updateMask": "description"
        }
    }]

Python

forms/snippets/update_form.py
from __future__ import print_function
from apiclient import discovery
from httplib2 import Http
from oauth2client import client
from oauth2client import file
from oauth2client import tools

SCOPES = "https://www.googleapis.com/auth/forms.body"
DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1beta&key=<YOUR_API_KEY>&labels=FORMS_BETA_TESTERS"

store = file.Storage('credentials.json')
creds = None
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secrets.json', SCOPES)
    creds = tools.run_flow(flow, store)

form_service = discovery.build('forms', 'v1beta', http=creds.authorize(
    Http()), discoveryServiceUrl=DISCOVERY_DOC,static_discovery=False)

form = {
    "info": {
        "title": "Update metadata example for Forms API!",
    }
}

# Creates the initial Form
createResult = form_service.forms().create(body=form).execute()

# Request body to add description to a Form
update = {
    "requests": [{
        "updateFormInfo": {
            "info": {
                "description": "Please complete this quiz based on this week's readings for class."
            },
            "updateMask": "description"
        }
    }]
}

# Update the form with a description
question_setting = form_service.forms().batchUpdate(formId=createResult["formId"], body=update).execute()

# Print the result to see it now has a description
getresult = form_service.forms().get(formId=createResult["formId"]).execute()
print(getresult)

Add an item

The following example shows how to add new content to a form. When adding new content, you must provide a location with an index where new content should be inserted. For instance, a location with index 0 will insert the content at the beginning of the form.

REST

To add an item to the form, call the batchUpdate() method with the form ID and the item's information and desired location.

Sample request body

"requests": [{
    "createItem": {
        "item": {
            "title": "Homework video",
            "description": "Quizzes in Google Forms",
            "videoItem": {
                "video": {
                     "youtubeUri": "https://www.youtube.com/watch?v=Lt5HqPvM-eI"
                }
            }},
        "location": {
          "index": 0
        }
}]

Python

forms/snippets/add_item.py
from __future__ import print_function
from apiclient import discovery
from httplib2 import Http
from oauth2client import client
from oauth2client import file
from oauth2client import tools

SCOPES = "https://www.googleapis.com/auth/forms.body"
DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1beta&key=<YOUR_API_KEY>&labels=FORMS_BETA_TESTERS"

store = file.Storage('credentials.json')
creds = None
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secrets.json', SCOPES)
    creds = tools.run_flow(flow, store)

form_service = discovery.build('forms', 'v1beta', http=creds.authorize(
    Http()), discoveryServiceUrl=DISCOVERY_DOC,static_discovery=False)

form = {
    "info": {
        "title": "Update item example for Forms API",
    }
}

# Creates the initial Form
createResult = form_service.forms().create(body=form).execute()

# Request body to add a video item to a Form
update = {
    "requests": [ {
        "createItem": {
            "item": {
                "title": "Homework video",
                "description": "Quizzes in Google Forms",
                "videoItem": {
                    "video": {
                         "youtubeUri": "https://www.youtube.com/watch?v=Lt5HqPvM-eI"
                    }
                }
            },
            "location": {
                "index": 0
            }
        }
    }
    ]
}

# Add the video to the form
question_setting = form_service.forms().batchUpdate(formId=createResult["formId"], body=update).execute()

# Print the result to see it now has a video
result = form_service.forms().get(formId=createResult["formId"]).execute()
print(result)