Skip to Content

Test SAP API Business Hub APIs with curl

Try out an SAP API locally on your machine.
You will learn
  • How to use command line tools like curl and jq with REST / OData APIs locally.
Prathibha-MouliPrathibha GCFebruary 1, 2022
Created by
January 12, 2017
Contributors
Prathibha-Mouli

Prerequisites

  • Make sure you have the command line tools curl and jq installed.
  • Windows instructions: curl and jq
  • MacOS/Unix instructions: jq (curl should already be available)

You have already discovered the SAP API Business Hub and accomplished testing an API on the website. In this tutorial, you will learn how to use command-line tools like curl and jq to test REST/OData APIs locally on your machine.

  • Step 1

    Go to https://api.sap.com and navigate to the Campaign API.

    Campaign API
  • Step 2

    Select GET/CampaignCollection method under API Reference. Glance through the GET/CampaignCollection to understand the parameters. The SAP API Business Hub provides you with starter code in a variety of languages and tools for each API.

    Get/CampaignCollection method

    Choose Try Out to proceed further.

    Try Out

    The API call starter code is available in various languages like JavaScript and Swift. You will be using the curl code in this example.
    Choose Code Snippet and select the Curl section to view the generated code. This personalized snippet already includes your unique API key.

    Curl Code

    Copy the highlighted curl code to the clipboard.

    copy the code
  • Step 3

    Paste the content of the clipboard in a terminal or command line window. Hit Enter to run the command. It may take a few seconds before data is seen on your screen.

    You can use CTRL+C to stop the command from running.

    terminal with code copied

    You’ll notice that the output is not formatted. For this, you can pipe the output of curl to jq to format it.

    terminal with code and jq added

    The formatted output represents the results of a simple call to the Campaign Collection API endpoint.

    Formatted output data
  • Step 4

    You can limit the number of results retrieved with the $top value provided in the Parameter section of the GET/CampaignCollection method. Switch back for a moment from the Code Snippet section to the Parameters section in the Try Out area and modify the value of the $top parameter to request just 3 results.

    You can then switch back to the Code Snippet section, and copy the updated code from the Curl section to paste it into your terminal. When you execute the curl command, and pipe the output into jq again, you should see a nicely formatted list of three campaigns.

  • Step 5

    Besides the $top parameter, you can add multiple parameters to the Query String of your API call.
    Now add in a second parameter for $select. You only want the StatusText and Status for the top 3 users. Use the same approach as before - switch to the Parameters section, specify the two values for the $select parameter using the drop down menu, then switch back to the Code Snippet section and copy the Curl content again. It should look something like this:

    bash
    Copy
    curl --request GET \
      --url 'https://sandbox.api.sap.com/sap/c4c/odata/v1/c4codataapi/CampaignCollection?%24select=Status%2CStatusText&%24top=3' \
      --header 'APIKey: your-api-key-here' \
      --header 'Accept: application/json' \
      --header 'DataServiceVersion: 2.0' \
    
    #API endpoint for API sandbox
    

    Notice how the parameters are encoded in the query string.

    Paste the entire command into your terminal again. This time the output is vastly reduced:

    text editor with parameter added

    You have now learnt how to use code snippets productively.

    Apart from the __metadata property, how many properties do each of the objects in the results array have?

Back to top