This tutorial demonstrates how to construct and execute an API request directly from your computer’s terminal or command line, using the curl command. The command line is a basic program offered on all computers.
To start, open a command line software. For Windows, this might be called “Command Prompt” and for Mac or Linux, it might be called “Terminal”.
For Windows users, the command line will open in a particular drive, such as C: for the C drive. To change drives, type a different letter followed by a colon (G:) and hit Enter.
For Mac/Linux users, the command line will open in a folder. To check the path to the current folder, use the pwd command.
For this example, we suggest using the cd command to navigate to a folder you want to store the output in (such as DSID) and creating a new folder (mkdir my_api_requests) in your file explorer.
Start by typing curl after the > symbol.
Headers are a way to pass metadata about a request to the API. In this case, you can specify that you want to provide input data in JSON format. Add a space and enter the following text:
Your current request now looks like this:
To specify the type of API request, add another space and type “--request POST”:
Your current request now looks like this:
Add the --data tag before adding raw JSON data, followed by a space.
To add the data inJSON directly into the command line (see Appendix for using an input file instead), start with a set of square [ ] and curly { } brackets, and paste in the data above, separating each field by a comma.
Next, add double quote marks before and after the square brackets. For each quotation mark inside the query, add a backslash to escape it: \". This allows it to recognize the inner quote marks as well as the ones surrounding the whole query.
Your current request looks like this:
Finally, add the URL for the DSID calculator API endpoint: https://dsid-api-dev.app.cloud.gov/v1/calculators
The full curl input should now look like this. Use the right arrow key to get to the end of the text and hit enter to submit the request.
The output should return from the DSID API in JSON format, right in your command window.
This output can become long for larger requests. To output the results to a file, you can add a > symbol and a filename to the end of the request:
See this example of what your output file should look like: output_from_curl.json
For larger requests, you may choose to use an input file instead of pasting the raw JSON data in like you did in Step 4.
To do so, first store your input fields in a JSON file such as “input.json”. Then add the data using double quotes, an @ symbol and your filename:
The rest of the request can remain the same:
You can now work with this output in your preferred software program. See the Converting JSON Data API page for importing into Excel.