Understanding how APIs Work in Business Accelerator Hub

Objective

After completing this lesson, you will be able to learn how the APIs work

APIs

How the APIs Work - SAP Business Accelerator Hub

In this unit, we will explain how the APIs work. The SAP Business Accelerator Hub is the place to find all the APIs for farming processes and services from plan-to-harvest that are available for SAP Intelligent Agriculture.

You can access the SAP Intelligent Agriculture Overview on SAP Business Accelerator Hub here: SAP Business Accelerator Hub: Overview | SAP Intelligent Agriculture

Prerequisites

The prerequisites are as follows:

  • You must understand what an API is
  • You must understand the structure details of the APIs and its prerequisites
  • You must understand OpenID Connect Protocol and Password Grant Type topics

In general, when you are working with and consuming APIs from the SAP Business Accelerator Hub, there are basic principles to be understood that apply across SAP solutions and follow market standards. Here are some pieces of important information and prerequisites to keep in mind:

  • You must understand what an API is. Refer to the following help documentation on APIs: APIs I SAP Help Portal
  • You must understand the structure details of the APIs and its prerequisites. Refer to the following help documentation on API Details: API Details I SAP Help Portal
  • You must understand the Using the Resource Owner Password Grant (RFC 6749) with the OpenID Connect Protocol (RFC 7591) topic, since it is they are necessary for authentication and getting the access token to be used in the API requests.
  • You must have the following $credentials at hand (example of CURL request):

    Code Snippet
    1234567891011
    curl --silent \ --request POST \ --header 'Content-Type: application/x-www-form-urlencoded’ \ --data-urlencode "client_id=$client_id" \ --data-urlencode "client_secret=$client_secret" \ --data-urlencode "username=$user" \ --data-urlencode "password=$password" \ --data-urlencode "login_hint=$login_hint" \ --data-urlencode "grant_type=password" \ --location 'https://$tenant.authentication.$region.hana.ondemand.com/oauth/token"

Let's look at the API information provided in the SAP Business Accelerator Hub (https://api.sap.com/package/SAPIntelligentAgriculture/overview) in more details.

Overview Page

The overview page provides you with support information such as the component to be used when creating tickets (LOD-AG-FARM), as well as a link to the product documentation.

Screenshot of the SAP Business Accelerator Hub webpage for SAP Intelligent Agriculture. The page has a header with navigation options like Explore, Resources, Discover Integrations, and Partner with Us. Below is the Overview section. The About section indicates the vendor as SAP with the version as Beta, last modified on 04 Oct 2023, and support info as LOD-AG-FARM. Description mentions enabling agribusinesses to improve farming efficiency through digitalization. There's a Documents section featuring a link to the SAP Intelligent Agriculture Product Documentation, version 1.0.0. Business tags include Product (SAP Intelligent Agriculture), Line of Business (Sustainability, Supply Chain), and Industries (Wholesale Distribution, Retail, Chemicals, Industrial Machinery and Components, Consumer Products). There are options to mark as favorite and subscribe, with a chat option labeled Beta.

SAP Business Accelerator Hub: APIs

On the ODATA V4 API, you can see all the services available for SAP Intelligent Agriculture.

Screenshot of the SAP Business Accelerator Hub webpage focused on ODATA V4 API services for SAP Intelligent Agriculture. It displays 12 out of 47 services on page 1 of 4. Services include Area Service, Brand Service, Building Service, Climate Zone Service, Container Service, Crop Pattern Service, Crop Service, Crop Zone Service, Cropping System Service, and Default Resource Service. Each service card shows an option to explore via ODATA V4 API with descriptions like Maintain Areas and Area Types for Area Service. All services are listed as Version Beta with a Beta tag. Options to mark as favorite, subscribe, and a chat labeled Beta are visible. Navigation and search functionalities are at the top.

Note

V4 refers to the version of the ODATA component in use.

As an example, let's look at the Area Service in more details.

SAP Business Accelerator Hub: API Overview Example

On the Overview page, you have access to the business documentation that provides a functional explanation on the attributes and functionalities of this API specifically.

Screenshot of the SAP Business Accelerator Hub webpage for Area Service under SAP Intelligent Agriculture. The Overview section introduces the Area Service APIs for maintaining areas and area types, allowing retrieval of localized API data versions. Buttons are available for View the API Reference, Check Schema View, and Try Out. The service status is Beta, type ODATA V4, last modified on 04 Oct 2023, version Beta, and direction is inbound. Documentation includes Business Documentation. The API Resources section lists API Specification, Authentication Methods, and Configuration Details for JSON, YAML, and EDMX formats. The page has navigation options for API Reference, Schema View, SAP Cloud SDK, and Try Out. There is a prompt to show the API key and a chat option labeled Beta.

The API Resources include the following:

  • API Specification related to the open API protocols
  • Authentication Methods used (for example, OAuth 2.0 Application Flow)
  • Configuration Details - this is where you can copy the URL to consume this API

SAP Business Accelerator Hub: API Reference

The API Reference page shows all the entities available for this service (for example, Areas and Area Types), as well as the operations that can be performed for each entity (for example, Get, Post, Patch, Delete, and so on).

Screenshot of the SAP Business Accelerator Hub webpage showing the API Reference section for Area Service under SAP Intelligent Agriculture. The page lists API endpoints with actions such as GET, POST, PATCH, and DELETE for managing areas. These include retrieving a list of areas, creating a single area, reading, changing, and deleting a single area, and retrieving a localized version of an area. Navigation tabs for Overview, Schema View, SAP Cloud SDK, and Try Out are available. A Try Out button is visible for testing APIs. Options to show the API key and chat labeled Beta are also present.

SAP Business Accelerator Hub: Schema View

The Schema View provides details on the data requirements to fulfill each attribute and the information that is required to load data.

Screenshot of the SAP Business Accelerator Hub webpage showing the Schema View section for Area Service under SAP Intelligent Agriculture. The schema details elements and structure for the Area object used in the API. Attributes include validFrom, validTo, identifier, description, areaTypeID, areaADM, and localized among others, indicating data types and restrictions. Navigation options are visible for Overview, API Reference, SAP Cloud SDK, and Try Out. There's an option to show the API key and a chat labeled Beta.

SAP Business Accelerator Hub: Try out/Sandbox

Finally, the Try Out page enables you to use the SAP Business Accelerator Hub to run requests and test the APIs in SAP Intelligent Agriculture without requiring the application.

Screenshot of the SAP Business Accelerator Hub webpage in the Try Out section for Area Service under SAP Intelligent Agriculture. The interface allows users to test the GET request for /Areas. It includes parameter fields such as $top, $skip, $search, $filter, $count, $orderby, and $select with corresponding entry boxes and descriptions. A sidebar lists API methods like GET, POST, PATCH, and DELETE for areas. Options are available to select an environment and a Run button is displayed. Tabs for Overview, API Reference, Schema View, and SAP Cloud SDK are visible, along with a chat option labeled Beta.

Under Select Environment, you can add a new environment to be used.

Before we start, it's important to understand how to read the API information provided on the SAP Business Accelerator Hub.

Let's go back to the Area schema view as an example.

Mandatory vs. Optional Information for Attributes

How do you read the API information provided in the schema view?

The Schema View tab provides the list of attributes available for each data entity, including the type of data required and other relevant information.

Screenshot of the Schema View section for Area Service under SAP Intelligent Agriculture. It displays the schema for areas when creating a record. Highlighted are attributes validFrom, described as a string in date-time format with an example, and name, described as a string with a maximum length of 255. Other attributes include description, areaTypeID, areaSize, and localized, each with nested details. Navigation tabs for Overview, API Reference, and Schema View are visible at the top.

For example, the 'Name' attribute requires string data that can have a maximum length of 255 characters. On the other hand, 'validFrom' requires string($date-time) data and an example of the expected data is provided.

To identify whether an attribute is mandatory (when creating a new record for example), you need to check the attribute information:

  • nullable: true = Optional
  • ﹤no information﹥ = Mandatory

For example, 'Name' is mandatory, while 'validFrom' and 'areaSize' are optional attributes.

Mandatory vs. Optional Information for Entity-Dependency

The same concept applies to the Entity Dependency, which means an attribute may require an entity to have been pre-created. For instance, 'areaType_ID' requires a uuid to be provided, so the Area Type should be created before the Area entity.

Screenshot of the Schema View section for Area Service under SAP Intelligent Agriculture. It shows the schema for creating an area record. Highlighted attributes are areaType_ID and areaUOM_ID, both defined as string GUIDs with example values. Other attributes include validFrom, validTo, description, areaSize, and localized, each with nested details. Navigation tabs for Overview, API Reference, and Schema View are visible at the top.

To identify whether an Entity-Dependency is mandatory, you need to check the information in the Schema View tab.

  • nullable: true = Optional
  • <no information﹥= Mandatory

For example, to create an Area the 'areaType_ID' attribute is mandatory, while the 'areaUOM_ID' is optional.

How to Consume the APIs

Now that we understand the prerequisites and how to read the APIs in the Business Accelerator Hub, let's look at how to consume the APIs.

  • Authenticate
    • <USERNAME> and <PASSWORD>
    • <CLIENT-ID-FROM-BROKER> and <CLIENT-SECRET-BROKER> from the BTP subaccount Table displaying service instance details for SAP Intelligent Agriculture. The instance named test-service-broker is shown. Details include the service as SAP Intelligent Agriculture, plan as standard, runtime environment as Other Environments, and one service binding under credentials. The status is marked as Created. Links for Cloud Foundry, Kyma/Kubernetes, and Other environments are visible above the table.
  • Get the Access Token
  • Run the API Request (Get, Post, Patch, etc.)

General Tools

You can use various tools to consume REST APIs. As mentioned, you can try out the SAP Intelligent Agriculture APIs using the SAP Business Accelerator Hub (https://help.sap.com/docs/business-accelerator-hub/sap-business-accelerator-hub/trying-out-apis) for sandbox or productive systems, and see how they work.

Alternatively, you can test the APIs using CURL (https://curl.se/), Insomnia (https://insomnia.rest), or any other tool of your choice.

For the purpose of this e-Learning, we will use the simplest tool (CURL), which is a pure text-based tool and will make it easy for you to understand the commands and ultimately convert the CURL request into another language of your preference (if needed).

Note

The content provided in this e-Learning course is for informational purposes only. We do not advocate the use of any specific tools referenced, displayed, or mentioned here.

Getting Started

There are 3 basic steps to consume the APIs:

  1. Authenticate
  2. Get the Access Token
  3. Run the API Request (Get, Post, Patch, and so on)

How to Authenticate

To access all the APIs, you will need the following:

  • Your ﹤USERNAME﹥and ﹤PASSWORD﹥, which are the same that you received when your user was initially created in IAS (as described in previous units) and that you use to log into the SAP Intelligent Agriculture application.
  • The ﹤CLIENT-ID-FROM-BROKE﹥ and ﹤CLIENT-SECRET-BROKER﹥ information, which are stored in the service key bound to the Service broker created for your SAP BTP subaccount. Follow the steps below to get the client Id and client secret information from the SAP BTP subaccount within the SAP BTP Cockpit, or request the information from your subaccount administrator.
  1. Go the BTP SubaccountInstances and Subscriptions and go to the service broker instance details (refer to previous figure, How to Consume the APIs)
  2. Open the service key bound to the Service broker created for that subaccount
  3. Look for the clientid and clientsecret variables. Copy them.

To get more details on API Authentication go to: API Authentication | SAP Help Portal

How to Get the Access Token

The next step is to obtain the Access Token, which is required to run the API request. Here's an example of a CURL command to get the OAuth Token:

Code Snippet
123456789
curl --request POST \ --header 'Content-Type: application/x-www-form-urlencoded’ \ --data-urlencode 'client_id=﹤CLIENT-ID-FROM-BROKER﹥’ \ --data-urlencode 'client_secret=﹤CLIENT-SECRET-BROKER﹥’ \ --data-urlencode 'username=﹤USERNAME﹥’ \ --data-urlencode 'password=﹤PASSWORD﹥’ \ --data-urlencode 'login_hint={"origin":"sap.custom"}’ \ --data-urlencode 'grant_type=password’ \ --location 'https://﹤TENANT-NAME﹥.authentication.eu10.hana.ondemand.com/oauth/token’

Note

Change and replace the ﹤VARIABLES﹥ accordingly.

How to Run a Request

There are different ways to fulfill the necessary information for the APIs.

  1. Here's an example on how you can execute a GET Request using a CURL command:"https://﹤TENANT﹥.﹤ORBIT﹥-﹤REGION﹥.ia.agribusiness.cloud.sap/api"

    Export the environment variables (example):

    • export TOKEN="﹤ACCESS-TOKEN"﹥
    • export ENDPOINT=" https://{baseUrl}.cfapps.{region}.hana.ondemand.com/api/farm-and-field-api/v1/odata/v1/FarmAndFieldService/Farms"

    Note

    • "﹤ACCESS-TOKEN﹥" – copy the access token field that is returned after running the command mentioned in Step 2.
    • https://{baseUrl}.cfapps.{region}.hana.ondemand.com/api/farm-and-field-api/v1/odata/v1/FarmAndFieldService/Farms – The API URL can be obtained in the SAP Business Accelerator Hub under Overview -﹥ Configuration details, as explained in the previous unit.

    Execute the request:curl --request GET --header "Authorization: Bearer $TOKEN" --location $ENDPOINT

  2. Here's an example on how to execute a PATCH Request for creating a new farm using a CURL command:

    Export the environment variables (example):export TOKEN="﹤ACCESS-TOKEN﹥"export ENDPOINT="

    https://{baseUrl}.cfapps.{region}.hana.ondemand.com/api/farm-and-field-api/v1/odata/v1/FarmAndFieldService/Farms(FARM-FIELD)

    "export DATA='{"description": "Farm test 1"}'

    Note

    • "﹤ACCESS-TOKEN﹥" - copy the access token field that is returned after running the command mentioned in Step 2.
    • https://{baseUrl}.cfapps.{region}.hana.ondemand.com/api/ farm-and-field-api/v1/odata/v1/FarmAndFieldService/Farms" - The API URL can be obtained in the SAP Business Accelerator Hub under Overview → Configuration details, as explained in the previous unit.

Execute the request:

curl --request PATCH --header "Content-Type: application/json" --header "Authorization: Bearer $TOKEN" --data $DATA --location $ENDPOINT

Step-by-Step Guide: Manual Execution Option

The SAP Intelligent Agriculture Team has developed a simple tool that provides a step-by-step guide on how to consume the APIs.

The API Shell Suite for SAP Intelligent Agriculture is accessible here: https://github.com/sap-contributions/api-shell-suite. It will be explained in further details in the next section.

Illustration showing the steps for interacting with an API. The process is divided into three sections: Authenticate, Get Access Token, and Run GET request. 1. Authenticate: Displays a shell script setup with sections to set user credentials and parameters including user, password, tenant, region, client_id, client_secret, and login_hint. 2. Get Access Token: Shows cURL commands to make a POST request to obtain an access token, using the parameters set in the Authenticate step. 3. Run GET request: Displays a cURL command to perform a GET request on an API endpoint using the access token obtained earlier. A disclaimer notes that commands might have been updated and advises checking the API Shell Suite for the latest details. Additionally, a file directory snippet shows two files, areas.sh and farms.sh, with commit messages and timestamps from two months ago.
Disclaimer: The API Shell Suite aims to provide guidance and support you during testing and trying out of the APIs. While it may be enhanced over time, it's not an official tool delivered with the solution and there is no commitment from SAP that the information reflects the latest development. It should not be used within your productive environments.

Manual Execution Option: https://github.com/sap-contributions/api-shell-suite/tree/main#manual-execution-option

Within the API Shell Suite, the manual-execution-apis folder (available here: https://github.com/sap-contributions/api-shell-suite/tree/main/app/manual-execution-apis), contains a set of files that are designed for manual copying and executing in your tool of choice (for example, CURL).

You can go through each file and command, understand its purpose, and execute it step-by-step to consume the API. Always ensure you understand the implications of a command before executing it.

Open the Farm.sh file to see an example on how to run a GET request for the Farm & Field service API.

The first 2 steps support authentication into the system, as previously explained.

  • Step 1: is about setting the Username and Password credentials
  • Step 2: is for entering the Client ID, Client Secret and tenant parameters

    → Simply copy the commands in each step and replace the placeholder information with the relevant information for your case.

    When this is authenticated, you need to get the Access Token by copying the exact commands in steps 3-5 sequentially.

    When the Access Token is granted, steps 6-8 outline the commands necessary for running the GET Request.

  • Step 6 is composed of a variable: api_url and a string: https://{baseUrl}.cfapps.{region}.hana.ondemand.com/api/farm-and-field-api/v1/odata/v1/FarmAndFieldService/Farms.

    The API URL can be obtained in the SAP Business Accelerator Hub under Overview → Configuration details, as explained in the previous unit

  • Step 7 is about executing the GET request.
  • Step 8 is about showing the results obtained from the GET request, which in this case will be the list of farms available in the system.

You have now seen how to consume an API. Let's move on to loading data in the system.

Log in to track your progress & complete quizzes