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

Create a form or quiz

This page describes how to perform these tasks involving forms:

  • Create a new form
  • Duplicate an existing form
  • Convert a form to a quiz

Before you begin

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

  • Complete authorization/authentication and credentials setup in the Early Adopter Program instructions.
  • Read the Forms API overview.

Create a new form

The initial creation of a form only requires a title field—any other fields in the request will be ignored. To build out the content and metadata of a form or make updates, use the batchUpdate() method. See Update a form or quiz for more information.

REST

Call the forms.create() method with only a title.

Sample request body

{
  "info": {
      "title": "My new form"
  }
}

Python

forms/snippets/create_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/drive"
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": "My new form",
    },
}
# Prints the details of the sample form
result = form_service.forms().create(body=form).execute()
print(result)

Duplicate an existing form

You can duplicate an existing form with the Google Drive API to make content re-use easier. You can find the form ID in a Google Forms URL:

https://docs.google.com/forms/d/FORM_ID/edit

REST

Call the Google Drive API's files.copy() method with the ID of the form you want to copy.

Python

forms/snippets/duplicate_form.py
from __future__ import print_function
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials

# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/drive']

def main():
    """Shows copy file example in Drive v3 API.
    Prints the name, id and other data of the copied file.
    """
    creds = None
    if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'client_secrets.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.json', 'w') as token:
            token.write(creds.to_json())

    service = build('drive', 'v3', credentials=creds)

    # Call the Drive v3 API
    origin_file_id = '1ox-6vHFeKpC6mon-tL5ygBC8zpbTnTp76JCZdIg80hA' # example ID
    copied_file = {'title': 'my_copy'}
    results = service.files().copy(
        fileId=origin_file_id, body=copied_file).execute()
    print(results)

if __name__ == '__main__':
    main()

Convert a form to a quiz

To create a quiz, first create a form as described above, then update the form's settings. The update requires the form ID.

REST

Call the batch.update() method on an existing form to set the isQuiz setting to true.

Sample request body

{
  "requests": [
    {
      "updateSettings": {
        "settings": {
          "quizSettings": {
            "isQuiz": True
          }
        },
       "updateMask": "quizSettings.isQuiz"
      }
    }
  ]
}

Python

forms/snippets/convert_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": "My new form",
    }
}

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

# JSON to convert the form into a quiz
update =  {
 "requests": [
    {
      "updateSettings": {
        "settings": {
          "quizSettings": {
            "isQuiz": True
          }
        },
       "updateMask": "quizSettings.isQuiz"
      }
    }
  ]
}

# Converts the form into a quiz
question_setting = form_service.forms().batchUpdate(formId=result["formId"],
                                                    body=update).execute()

# Print the result to see it's now a quiz
getresult = form_service.forms().get(formId=result["formId"]).execute()
print(getresult)

Next steps

Here are a few next steps you might try: