U.S. flag

An official website of the United States government

Access the API: Excel

This tutorial will demonstrate how to execute an API request using Excel’s Power Query Editor. There are two main types of API requests in Excel: GET requests and POST requests.

Part 1: GET request

GET Requests are useful for performing small requests by passing input fields to the API using a URL. See the “Web Browser” for a detailed explanation of a GET request for the DSID API.

To build a GET request in Excel, first go to the Data tab → Get Data → From Other Sources → From Web

DSID API Example Response

The “From Web” popup will appear on your screen. Select “Advanced”.

DSID API Example Response

Example: you want to estimate calcium levels in Adult Multivitamin/Minerals (MVMs) based on a label amount of 200 mg.

In the URL Parts section, click the “Add Part” button 3 times, so that it shows 5 parts.

DSID API Example Response
Populate the URL fields with the following information:
  • Base URL, with a ? at the end: https://dsid-api-dev.app.cloud.gov/v1/calculators?
  • Study code: study_code= “01”
  • Ingredient: &ingredient=calcium
  • Label amount: &label_amount=200
  • Units: &unit=“mg”
DSID API Example Response

In the HTTP request header parameters section, click the “Add header” button once to allow two headers. Use the dropdown options to select the “Accept” and “Content-Type” headers. Fill both text boxes with the text application/json and Click OK.

DSID API Example Response

You may now see a screen titled “Access Web Content” that looks like the image below; this is used in case an API requires user login or authentication. The DSID API is public and does not require user credentials. You may leave this as “Anonymous” and click Connect.

DSID API Example Response

You should see the Excel Power Query Editor open and your results shown in a table.

DSID API Example Response

To convert this result from JSON into a table, click the “Into Table” or “To Table” button in the top-left of the screen.

DSID API Example Response
DSID API Example Response

The final step is to “Close and Load” to return to your Excel window and view your final table.

DSID API Example Response
DSID API Example Response

Note that the study_code, ingredient, and unit fields are left-aligned since they are text values.

Part 2: POST request

POST requests are useful for more complex, larger queries to the DSID API. These requests may involve ingredients with multiple label amounts, more than one ingredient from a study, or even data from multiple studies.

First, open a blank Excel sheet and go to the Data tab → Get Data → From Other Sources → Blank Query.

DSID API Example Response

This will open the Power Query Editor.

DSID API Example Response

Click the Advanced Editor button to open the Advanced Editor popup window.

DSID API Example Response
DSID API Example Response

Delete any text in the Query text box so that it is completely blank. To create a custom function, copy the following code from the attached file:

DSID API Example Response

Make sure that the bottom left reads “No syntax errors have been detected” and has a green check mark.

DSID API Example Response

Click “Done.” The screen should now show a function setup and look like this:

DSID API Example Response

In the Properties on the right of the screen, change the name to send_api_request and hit Enter.

Now click “Close and Load” in the top left.

Enter your input data, using the four required fields – study_code, ingredient, label_amount, and unit. Ensure that the study_code column is formatted as text – either type an apostrophe first ('05) or right-click → Format Cells → Text.

DSID API Example Response

Highlight the range of values you have entered and go to Data → From Table/Range.

DSID API Example Response
DSID API Example Response

In the “Create Table” popup box, make sure to select the corresponding range for your data and check the “My table has headers” box. Click OK.

DSID API Example Response

This will re-open the Excel Power Query Editor and display your table.

DSID API Example Response

At this point, be sure to check whether the study_code field still shows values of “01” and not “1”. Excel may have converted it automatically to an integer, as shown below.

DSID API Example Response

If you see the “Changed Type” under APPLIED STEPS on the right hand side of the screen, click on the X next to it. You should now only see the Source step and the study_code should look like “01” in the table.

DSID API Example Response

Now, navigate to Add Column → Invoke Custom Function.

DSID API Example Response

In the “Invoke Custom Function” popup box, enter a name for the column and choose the send_api_request function query that you created earlier.

DSID API Example Response

Four new fields will appear asking which column of your table corresponds to each API input field. Choose the dropdown on the left and select “Column Name” and provide the corresponding columns for your table.

DSID API Example Response
DSID API Example Response

At this point, Excel may show a warning and popup about setting Privacy Levels (for first time power query users). The DSID API and data are public, so you can set the levels for “Current Workbook” and the DSID cloud.gov URL to “Public” and hit Save.

DSID API Example Response

The API results will appear as a new column (with the column name you specified) on the right of your table.

DSID API Example Response

Note: if any of your input fields have typos/errors, now is the time to fix them! Rows with errors will say “Error” in the “API Output” column and the header will have a red flag near it. Any errors may prevent the entire request from going through successfully.

Example error: Misspelling of “iodine” as “iodin”.

DSID API Example Response

To address any errors in data entry, click Close to exit the power query editor. It will then ask if you want to keep your changes, click Keep.

DSID API Example Response

Return to your table, and fix the spelling error. Then re-open the Power Query Editor through Data → Get Data → From Table/Range.

DSID API Example Response

Once you are back in the Power Query Editor, return to the table and query you were working on.

To separate the list into columns, click the DSID API Example Response to the right of the new “API Output” column and select “Expand to New Rows”. Click the DSID API Example Response icon again.

DSID API Example Response

Uncheck the box at the bottom next to “Use original column name as prefix.” You may also wish to unselect the columns used as input fields to avoid duplicates (study_code, ingredient, label_amount, unit). Once you have selected all the columns of results you want, click OK.

DSID API Example Response

You now have calculated results from the API. Scrolling can show you all of the values for your request. If a row has an invalid input field, it will return Error for all columns.

DSID API Example Response

The final step is to “Close and Load” to return to your Excel window and view your final table.

DSID API Example Response

The updated table will appear in the sheet you were working on.

DSID API Example Response