Replacing API Products for an existing Company App credential

Hi API Developers,

While Apigee's management API allows for users to append additional API Products to an existing Company App credential via this API call, some users may want the capability to overwrite instead.

curl -X POST -u $USERID:$USERPASSWORD https://api.enterprise.apigee.com/v1/organizations/$org/companies/$company/apps/$app/keys/$CONSUMER_... -H 'Content-Type: application/json' -d '{
 "apiProducts": [ "'$additional_product'" ]
}'

A POST operation typically 'updates' a resource, so what happens here is that the API Product is appended to the list of existing products already added to this credential.

If we wanted the $additional_product to replace the existing ones, we would technically need a PUT operation. However this is not supported.

The next best solution to this would be to DELETE the existing products from the key iteratively. Here is a bash script to automate the whole process. Assumption is there is only one credential for the given app, but it can easily be tweaked if that is not the case.

#!/bin/bash 
org=<org> 
company=<company> 
app=<app> 
new_product=<product> 


CONSUMER_KEY=`curl -u $USERID:$USERPASSWORD https://api.enterprise.apigee.com/v1/organizations/$org/companies/$company/apps/$app/ | jq -r .credentials[0].consumerKey` 


for apiproduct in `curl -u $USERID:$USERPASSWORD https://api.enterprise.apigee.com/v1/organizations/$org/companies/$company/apps/$app | jq -r .credentials[0].apiProducts[].apiproduct`; do 
curl -X DELETE -u $USERID:$USERPASSWORD https://api.enterprise.apigee.com/v1/organizations/gsc/companies/$company/apps/$app/keys/$CONSUMER_K... 
done 


curl -X POST -u $USERID:$USERPASSWORD https://api.enterprise.apigee.com/v1/organizations/$org/companies/$company/apps/$app/keys/$CONSUMER_... -H 'Content-Type: application/json' -d '{ 
"apiProducts": [ "'$new_product'" ] 
}'
Version history
Last update:
‎03-24-2019 06:22 PM
Updated by: