{"_id":"59308b22b28051002f66acf9","project":"55843604fd8d910d007b9502","version":{"_id":"558444ceafccfd0d00fcb2bb","forked_from":"55843604fd8d910d007b9505","project":"55843604fd8d910d007b9502","__v":60,"createdAt":"2015-06-19T16:35:26.435Z","releaseDate":"2015-06-19T16:35:26.435Z","categories":["558444cfafccfd0d00fcb2bc","558444cfafccfd0d00fcb2bd","55ad4ce733616a0d00599d2e","55ad4cef6aadf20d0015b764","55ad4cf36aadf20d0015b765","55ad4cfb24cf160d0013584f","55ad4d0024cf160d00135850","55ad4d0a24cf160d00135851","55ad4d0d24cf160d00135852","55ad4d126aadf20d0015b766","55ad4d1624cf160d00135853","55ad4d1933616a0d00599d2f","55ad4d2233616a0d00599d30","55ad4d2e24cf160d00135854","55d35b6bf77e6d0d00b1b092","55d3649a0168850d0073f14a","55d366d40168850d0073f15a","55d37fcff77e6d0d00b1b13f","55d383e50168850d0073f1e1","55d3ac26c336ec0d007c2251","55d3c51cb2330119009c31db","55d3c59bfe37111900e536f3","55d3c5a7fe37111900e536f4","55d3c5b4fe37111900e536f5","55d3c5d4fe37111900e536f6","55d3c5d6b2330119009c31df","55d3c5d71f478b170077c164","55d3c687b2330119009c31e4","55d3c6a4fe37111900e536f9","55d3c6befe37111900e536fa","55d3c6e8d2c66f0d00497f93","55d49dcfd7c16b2d007de905","55d4ca8f5082980d0009c79b","55d4cab9c95a3d2f0069ad3d","55d4d279c95a3d2f0069ad60","55d4d9355082980d0009c7e1","55d4f6b5988e130d000b3eb1","55d64dc8e60a2f0d00b88ecb","5627ca43fcbbc621004ec07d","56c64a0d8f98b50d0012c37c","56f1b8b13eb62a34003ea041","56f1b9df4476fb2200795e8c","57f6907dca5e5d1700039ae9","591dd06ca266c423002ec4ca","59234825e465c11900922518","5936f82eaa591e0027638d57","59972f54fd7078001992c136","599c6da8f180820025f14909","59b054613c3e1b0019cf27d9","59b1ceca2d6231003ad73e5f","59b1cf1857911600382e0dc4","59b1cf2730f3d60010c30ef7","59b1cf385d4b89003035441a","59b1cf5857911600382e0dc6","59bc2c4e26ac9b0010a8b753","59bc2ce20b3eb30010657b70","59f0c793ba3bc90030f413ab","59f0cd62f5ecda00325294b9","59fb55a8e8d0f600101aedc3","59fcb05c067f8d0028613f86","5a2af4a1bc5fba00283909c1"],"is_deprecated":false,"is_hidden":false,"is_beta":true,"is_stable":true,"codename":"","version_clean":"3.0.0","version":"3"},"category":{"_id":"59234825e465c11900922518","__v":0,"version":"558444ceafccfd0d00fcb2bb","project":"55843604fd8d910d007b9502","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2017-05-22T20:20:53.281Z","from_sync":false,"order":1,"slug":"using-the-qualtrics-api","title":"Using the Qualtrics APIs"},"user":"5919f13aff66b00f00f1948c","__v":0,"parentDoc":null,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2017-06-01T21:46:10.368Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":0,"body":"This document contains information about the libraries, language versions, and tools are needed to use the API samples. The samples use the installation's default packages or libraries unless that would result in cluttered or difficult-to-understand code.\n[block:callout]\n{\n  \"type\": \"warning\",\n  \"title\": \"API Token\",\n  \"body\": \"The sample code includes the API token in the code, which is not the recommended practice and is for educational purposes only. See [Protecting Your API Token](https://api.qualtrics.com/docs/authentication-1#protecting-your-api-token) for more information about best practices when using API tokens.\"\n}\n[/block]\n\n[block:api-header]\n{\n  \"title\": \"Python\"\n}\n[/block]\nMost of the samples were written for Python 3 (3.6.1).\n\n## Installing the requests library\n\nThe Python samples use the **requests** library, which simplifies constructing request objects and invoking REST APIs. If you have pip installed, you can use it to install **requests**:\n\n```\npip install requests\n```\n\nSome installations (usually those with both Python 2 and Python 3 installed) require you to use pip3 instead:\n\n```\npip3 install requests\n```\n\n(The pip command might also be called ```pip3.6``` or something similar.)\n\n## The requests_toolbelt Library\n\nIn addition, if a sample needs to create a multipart form for file uploads, you'll need the **requests_toolbelt** library. You can also use ```pip``` to install it.\n\nFor more information about using this library for uploading files, see [Uploading Data](http://toolbelt.readthedocs.io/en/latest/uploading-data.html).\n\n## Pretty Printing JSON with Python\n\nIf you want to make the JSON result you receive from the server more readable for debugging (by indenting braces and adding newlines), you can pipe it through the Python JSON pretty printer like this (Mac and Linux):\n\n```\ncat output.json | python -m json.tool\n```\n\nWindows:\n\n```\ntype output.json | python -m json.tool\n```\n[block:api-header]\n{\n  \"title\": \"Using the Code Samples with Your Installation\"\n}\n[/block]\nTo use the samples, you'll need at least two pieces of information:\n\n1. Your API token. See [Authentication](doc:authentication-1) for more details. All of the APIs require an HTTP header (`X-API-TOKEN`) that contains your API token. The sample code contains a placeholder for the token, `YOUR API TOKEN`, that should be replaced with your token.\n2. Your data center (also known as your organization ID). See [Finding Your Qualtrics IDs](doc:finding-qualtrics-ids) for more information. Your data center ID forms part of the URL you use to access the Qualtrics APIs. The samples show `YOUR DATACENTER` where you should replace it with your data center.\n\nLater samples use environment variables for configuration.\n\n- Q_API_TOKEN should be set to your API token\n- Q_DATA_CENTER should be set to your data center.\n\nIf you're using Target Audience, you need to also set Q_TA_POOL_ID to your Target Audience Pool ID.\n\nFor bash you'd set the environment variables like this:\n```\nexport Q_API_TOKEN=\"Your api token\"\nexport Q_DATA_CENTER=\"data center\"\nexport Q_TA_POOL_ID=\"pool id\"\n```\n\nFor more information about where to find these values, see [Finding Your Qualtrics IDs](https://api.qualtrics.com/docs/finding-qualtrics-ids) and [Base URL and Datacenter IDs](https://api.qualtrics.com/docs/root-url).","excerpt":"Notes on using the Qualtrics API samples","slug":"using-the-api-code-samples","type":"basic","title":"Using the API Code Samples"}

Using the API Code Samples

Notes on using the Qualtrics API samples

This document contains information about the libraries, language versions, and tools are needed to use the API samples. The samples use the installation's default packages or libraries unless that would result in cluttered or difficult-to-understand code. [block:callout] { "type": "warning", "title": "API Token", "body": "The sample code includes the API token in the code, which is not the recommended practice and is for educational purposes only. See [Protecting Your API Token](https://api.qualtrics.com/docs/authentication-1#protecting-your-api-token) for more information about best practices when using API tokens." } [/block] [block:api-header] { "title": "Python" } [/block] Most of the samples were written for Python 3 (3.6.1). ## Installing the requests library The Python samples use the **requests** library, which simplifies constructing request objects and invoking REST APIs. If you have pip installed, you can use it to install **requests**: ``` pip install requests ``` Some installations (usually those with both Python 2 and Python 3 installed) require you to use pip3 instead: ``` pip3 install requests ``` (The pip command might also be called ```pip3.6``` or something similar.) ## The requests_toolbelt Library In addition, if a sample needs to create a multipart form for file uploads, you'll need the **requests_toolbelt** library. You can also use ```pip``` to install it. For more information about using this library for uploading files, see [Uploading Data](http://toolbelt.readthedocs.io/en/latest/uploading-data.html). ## Pretty Printing JSON with Python If you want to make the JSON result you receive from the server more readable for debugging (by indenting braces and adding newlines), you can pipe it through the Python JSON pretty printer like this (Mac and Linux): ``` cat output.json | python -m json.tool ``` Windows: ``` type output.json | python -m json.tool ``` [block:api-header] { "title": "Using the Code Samples with Your Installation" } [/block] To use the samples, you'll need at least two pieces of information: 1. Your API token. See [Authentication](doc:authentication-1) for more details. All of the APIs require an HTTP header (`X-API-TOKEN`) that contains your API token. The sample code contains a placeholder for the token, `YOUR API TOKEN`, that should be replaced with your token. 2. Your data center (also known as your organization ID). See [Finding Your Qualtrics IDs](doc:finding-qualtrics-ids) for more information. Your data center ID forms part of the URL you use to access the Qualtrics APIs. The samples show `YOUR DATACENTER` where you should replace it with your data center. Later samples use environment variables for configuration. - Q_API_TOKEN should be set to your API token - Q_DATA_CENTER should be set to your data center. If you're using Target Audience, you need to also set Q_TA_POOL_ID to your Target Audience Pool ID. For bash you'd set the environment variables like this: ``` export Q_API_TOKEN="Your api token" export Q_DATA_CENTER="data center" export Q_TA_POOL_ID="pool id" ``` For more information about where to find these values, see [Finding Your Qualtrics IDs](https://api.qualtrics.com/docs/finding-qualtrics-ids) and [Base URL and Datacenter IDs](https://api.qualtrics.com/docs/root-url).