ODK Central Data Pull via API - Pull all Submission Columns

This python code works for data pull except that it limits the output to the columns instance, submitter, device id, created at, updateat, reviewstate. What tweak do I need to pull all columns in the submissions pane.

def fetch_odk_data(base_url, username, password, project_id, form_id):
    # Set the endpoint for fetching the data
    endpoint = f"{base_url}/v1/projects/{project_id}/forms/{form_id}/submissions"

    # Create a session to handle authentication
    session = requests.Session()
    session.auth = (username, password)

    all_submissions = []

    try:
        # Make a GET request to fetch the data
        response = session.get(endpoint)
        response.raise_for_status()  # Raise an error for bad responses

        # Parse the JSON data
        data = response.json()

        # Debugging: print the structure of the response
        print("Response Data Structure:", json.dumps(data, indent=4))

        # Ensure we are correctly accessing submissions
        if 'submissions' in data:
            all_submissions.extend(data['submissions'])
        else:
            print("No 'submissions' key found in the response.")

        # Check if there's pagination and fetch all pages if necessary
        while 'nextPageToken' in data:
            next_page = f"{endpoint}?pageToken={data['nextPageToken']}"
            response = session.get(next_page)
            response.raise_for_status()
            next_data = response.json()

            # Debugging: print the structure of the next page response
            print("Next Page Data Structure:", json.dumps(next_data, indent=4))

            if 'submissions' in next_data:
                all_submissions.extend(next_data['submissions'])
            else:
                print("No 'submissions' key found in the next page response.")

        # Save the complete submission data to a file
        with open('locator_form.txt', 'w') as file:
            json.dump(all_submissions, file, indent=4)  # Write the list of submissions in a formatted way

        print("Data successfully written to locator_form.txt")

        return all_submissions

    except requests.exceptions.RequestException as e:
        print(f"An error occurred: {e}")
        return None

# Example usage
if __name__ == "__main__":
    base_url = ODK_URL  # Replace with your ODK Central base URL
    username = ODK_USERNAME  # Replace with your username
    password = ODK_PASSWORD  # Replace with your password
    project_id = ODK_PROJECT_ID  # Replace with your project ID
    form_id = ODK_FORM_ID  # Replace with your form ID

    data = fetch_odk_data(base_url, username, password, project_id, form_id)
    if data is not None:
        print(data)

Hello @Francis_Opiyo,

To access your data, you will need to adjust the endpoint you are using. Update it to match the following format:

GET /v1/projects/{projectId}/forms/{xmlFormId}.svc/{table}

For example:

endpoint = f"{base_url}/v1/projects/{project_id}/forms/{form_id}.svc/Submissions.household_registration.member_details/"

Here, replace {table} with the name of your table. In this example, I used Submissions.household_registration.member_details/ as the table name, but you will want to substitute it with the specific path to your table.

For more details, you can check the documentation here:
ODK Central API: Data Endpoints

@Francis_Opiyo if you would like to use the pyodk library (docs), the code would look like:

from pyodk.client import Client
client = Client()
form_data = client.submissions.get_table(form_id="birds", project_id=8)
with open('locator_form.txt', 'w') as file:
    json.dump(all_submissions, file, indent=4)
1 Like

Je souhaite comprendre comment synchroniser les données de mon serveur ODK avec Google Sheets à travers l'API