SAP Home Learn Build Integrate Model Operate Extend with AI ConnectTutorial navigator Knowledge Graph API Devtoberfest Developer Advocates App Space

Manage my Account SAP Devs YouTube ↗ Learnings ↗ Community ↗ Provide Feedback ↗
Logout
⤢ Open full site

Configure SAP API Management policies to avoid CORS issues and generate an API Key

Learn how to set up API Management policies to avoid CORS issues when calling the API endpoint from SAP Build Apps (formerly SAP AppGyver), also you will learn how to apply security policies to enable the API consumption via an API Key.

Overview

🎓 advanced 20 min. SAP API ManagementAdvancedFree TierSAP Integration SuiteCloud IntegrationSAP Business Technology Platform

You will learn

  • How to set up API Management policies to avoid CORS issues when calling the API endpoint.
  • How to apply security policies to enable the API consumption via an API Key.
Mariajose Martinez M Mariajose Martinez April 13, 2023
Created by October 28, 2022
Contributors

Prerequisites

Prerequisites

Steps

Step 1 Set up the policy to avoid CORS issues

  1. Go to your SAP API Portal (in the main SAP Integration Suite portal).

  2. Create an API and select URL. Here you’re going to paste your IFlow endpoint from past tutorials. Give it a name: APIBestRunDemo. Give it a path: /https/salesOrder, and service type REST.

    Create the API
    Create the API

  3. After creating it, go to the Proxy Endpoint tab and add the following in this order, and save it:

    Add the route rules
    Add the route rules

  4. Now, go to Policies (if it doesn’t show up in the top-right of your screen, click on the 3 dots).

    Here you’ll add the required policies to avoid CORS issues while calling this API from SAP Build Apps and configure the API Key policy later on.

  5. On the Edit Mode, start adding the Policies as followed:

    In the PostFlow inside the Proxy Endpoint configuration, add an Assign Message policy as an OutgoingResponse and name it setCORS. Like this:

    Add the route rules
    Add the route rules

  6. Copy and paste this script:

    XML
    <!-- This policy can be used to create or modify the standard HTTP request and response messages -->
    <AssignMessage async="false" continueOnError="false" enabled="true" xmlns='http://www.sap.com/apimgmt'>
            <Set>
                <Headers>
                    <Header name="Access-Control-Allow-Origin">*</Header>
                    <Header name="Access-Control-Allow-Headers">set-cookie, origin, accept, maxdataserviceversion, x-csrf-token, authorization, dataserviceversion, accept-language, x-http-method, content-type, X-Requested-With, apikey</Header>
                    <Header name="Access-Control-Max-Age">3628800</Header>
                    <Header name="Access-Control-Allow-Methods">GET, PUT, POST, DELETE</Header>
                    <Header name="Access-Control-Expose-Headers">set-cookie, x-csrf-token, x-http-method</Header>
                </Headers>
            </Set>
            <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
            <AssignTo createNew="false" type="response">response</AssignTo>
    </AssignMessage>

    Notice that you’re already adding apikey as a header in the policy.

    You should look it like this:

    CORS Policy
    CORS Policy

  7. In the ProxyEndpoint click on + to add one. Name it preflight and in the condition string paste: request.verb == "OPTIONS". Like this:

    Preflight Condition String
    Preflight Condition String

    This is all you need to avoid CORS issues when calling our API from SAP Build Apps.

    On write “preflight” step, you may find a bug on the API Management UI, where the Update button stay on grey/disable. To avoid it and save your changes, you must create a dummy assign message anywhere, save it and then remove it.

Step 2 Create a Key Value Map for your SAP Cloud Integration Credentials
+
Step 3 Add the Verify API Key Policy
+
Step 4 Create an API Product and Subscribe to the Application
+
Step 5 Add additional tasks into your Integration Flow
+
Step 6 Check your knowledge
+
Step 7 One more time, check your knowledge
+

Resources

Discussion

Share feedback on this tutorial or join the conversation in SAP Community.

Submit detailed feedback Discuss in Community
Steps
Step 1 of 7
1. Set up the policy to avoid CORS issues 2. Create a Key Value Map for your SAP Cloud Integration Credentials 3. Add the Verify API Key Policy 4. Create an API Product and Subscribe to the Application 5. Add additional tasks into your Integration Flow 6. Check your knowledge 7. One more time, check your knowledge

Learn more →