cancel
Showing results for 
Search instead for 
Did you mean: 

CAP with external API service

JérémyChabert
Participant

Hello SAP community,

I have a question to submit.

We are using CAP to reach an API from ApiManagement hosted on Neo.

Our model is defined in package.json as follow
"cds":{    
    "requires : {            
        "orders": {          
             "kind": "odata",          
             "model": "srv/external/OrdersMetadata.json",          
             "credentials": {
                 "APIKey": <API_KEY> <--- we tried here
                 "requestTimeout": 10000,            
                 "destination": "dest_orders"          
             },          
             "pool": {            
                    "min": 1,            
                    "max": 10          
             }       
        }    
    }
}
We tried while working locally for now to provide the credentials (APIKey) in destination defined in a default-env.jsonfile
{
            "name": "dest_orders",
            "url": "https://<API_MANAGEMENT_DEST>/v0/orderTracking",
            "headers": {
                "APIKey": <API_KEY> <--- and we tried here
            }
}

but so far we are getting a 401 unauthorized and this error :
{  fault: 
    {    
        faultstring: 'Failed to resolve API Key variable request.header.APIKey',    
        detail: { errorcode: 'steps.oauth.v2.FailedToResolveAPIKey' }  
     }
}
So our guess is that APIKey is not forwarded with the request but we cannot find how to send the APIKey with the request.
We have tried to set req._.req.headers in an before handler on our service but it didn't consider it.
Do you have any suggestions ?

Accepted Solutions (1)

Accepted Solutions (1)

Hi,

setting headers is supported with the next release (approximately end of August). Please check the documentation here:
https://github.wdf.sap.corp/pages/cap/node.js/api#srv-emit

const tx = service.transaction()
const resultUsingHeaders = await tx.emit({ query: SELECT.from('Entity'), headers: { header1: 'content' } })

Best regards

Samuel

gregorw
Active Contributor
0 Kudos

Hi Samuel,

please be aware that this is an external forum and the external link is:

https://cap.cloud.sap/docs/node.js/api#srv-emit

Best regards
Gregor

JérémyChabert
Participant
0 Kudos

Hi Samuel, thanks for the tip !

Best regards,

Jérémy

Answers (1)

Answers (1)

heiko_witteborg
Explorer

Hi,

thanks for reporting this gap - indeed, we currently don't offer a possibility to set headers in the http calls to a rest API. We are in discussion on how to extend our API accordingly. We will of course keep you updated here (but I cannot give a timeline yet, sorry...).

Best regards,

Heiko

JérémyChabert
Participant
0 Kudos

Thanks for the input. I'll wait for your update then.

Best regards,

Jérémy

0 Kudos

Hi Heiko, iinside , dj.adams , mariusobert ,

So if we have to connect to external services like SuccessFactors from a CAP (SAP Cloud Application Model) nodejs project, how do we proceed? Can you please advise ? If you have any blogs, can you please share? The document at https://cap.cloud.sap/docs/guides/consuming-services#for-http-based-consumption is very vague.

Thanks and Regards,

Harish

gregorw
Active Contributor

Hi Harish,

as this question was specifically about the use of the api.sap.com APIKey together with CAP please post your question as a new topic.

Best regards
Gregor