Was this helpful?


Many policy types defined by Apigee Edge rely on 'resources'. Resources are the files that implement the code or configuration to be executed by a policy when attached to an API proxy. In some cases, as with JavaScript and JavaCallout, a policy simply defines an attachment point in an API proxy where some code should execute. A JavaScript or JavaCallout policy is a pointer to a resource.

These resources can be stored in any of three locations in an organization.

  • API proxy: When stored in an API proxy, resources are available only to that API proxy
  • Environment: When stored in an environment (for example, test or prod), the resource is available to any API proxy deployed in the same environment.
  • Organization: When stored in an organization, the resource is available to any API proxy deployed in any environment.

Although all the the examples in the Build APIs topics store resources and policies in the API proxy package, API Services also provides repositories for these resources at the organization and the environment level. By storing resources at the organization or the environment level, you enable re-use and sharing of code and configuration across the API proxies deployed in your organization and across different environments.

Resource types

Resource repositories support the following resource types:

  • jsc: compiled JavaScript, referenced by policies of type Javascript
  • java: Java classes, referenced by policies of type JavaCallout
  • py: Python scripts, referenced by policies of type Python
  • wsdl: WSDL files, referenced by policies of type MessageValidation
  • xsd: XML schemas, referenced by policies of type MessageValidation
  • xsl: XSLT transformations, referenced by policies of type XSL

The repositories are available at the following URIs:

/organizations/{org_name}/resourcefiles
/organizations/{org_name}/environments/{environment_name}/resourcefiles
/organizations/{org_name}/apis/{api_name}/revisions/{revision_number}/resources

For example, all JavaScript files available across an organization are stored under:

/organizations/{org_name}/resourcefiles/jsc

JavaScript stored in the /organizations collection is available to any API proxy running in any environment.

You can list all available resources by calling GET on the collection:

$ curl https://api.enterprise.apigee.com/v1/organizations/{org_name}/resourcefiles/{resource_type}

The following request lists all JavaScript resources at the organization level:

$ curl https://api.enterprise.apigee.com/v1/organizations/myorg/resourcefiles/jsc

The following request lists all JavaScript resources at the environment level, in the environment called prod:

$ curl https://api.enterprise.apigee.com/v1/organizations/myorg/environments/prod/resourcefiles/jsc

The following request lists all JavaScript resources in an API proxy revision (the most specific level):

$ curl https://api.enterprise.apigee.com/v1/organizations/myorg/apis/weatherapi/revisions/6/resourcefiles/jsc

Each request returns a list of resource names.

Sample response:

<List>
  <Item>JavaScriptMashUp</Item>
  <Item>JavaScriptClient</Item>
</List>

Populating resource repositories

Let's assume that you have a simple snippet of JavaScript that you need to make available to API proxies across your organization. The following sample JavaScript sets the HTTP request path to the value of the proxy.basepath variable. It's just one example of a resource that you might need to put in the repository.

request.setHeader("RequestPath", flow.getVariable("proxy.basepath"));

To create a resource, you call the POST method, submitting the body of the resource file, and identifying the resource's name and type as query parameters.

To make the JavaScript available to API proxies running in any environment in your organization:

$ curl -X POST -H "Content-type:application/octet-stream" -d \
'request.setHeader("RequestPath", flow.getVariable("proxy.basepath"));' \
https://api.enterprise.apigee.com/v1/organizations/myorg/resourcefiles?"name=pathSetter&type=jsc" \
-u myname:mypass

Sample response:

{
  "name" : "pathSetter",
  "type" : "jsc"
}

You can also upload resources as files from your local machine as follows:

$ curl -X POST -d @pathSetter.js \
https://api.enterprise.apigee.com/v1/organizations/myorg/resourcefiles?"name=pathSetter&type=jsc" \
-u myname:mypass

The JavaScript resource named pathSetter is now available to be referenced by policies of type JavaScript running in any environment.

For example, to attach the JavaScript to the Request PostFlow, create a policy called PathSetterPolicy.xml that references the file pathSetter.js:

<Javascript name='PathSetterPolicy' timeLimit='200'>
    <ResourceURL>jsc://pathSetter.js</ResourceURL>
</Javascript>

Then reference the policy in the Endpoint configuration:

<PostFlow>
  <Request>
    <Step><Name>PathSetterPolicy</Name></Step>
  </Request>
<PostFlow>

Resource name resolution

Apigee Edge resolves resource names from the most specific to the most general scope. Resource names are resolved "up the chain", from the API proxy level, to the environment level, to the organization level.

Let's say that you have populated the same resource in two different repositories — the organization and the environment:

$ curl -X POST -H "Content-type:application/octet-stream" -d \
'request.setHeader("RequestPath", flow.getVariable("proxy.basepath"));' \
https://api.enterprise.apigee.com/v1/organizations/myorg/resourcefiles?"name=pathSetter&type=jsc" \
-u myname:mypass
$ curl -X POST -H "Content-type:application/octet-stream" -d \
'request.setHeader("RequestPath", flow.getVariable("proxy.basepath"));' \
https://api.enterprise.apigee.com/v1/organizations/myorg/environments/prod/resourcefiles?"name=pathSetter&type=jsc" \
-u myname:mypass

Let's take a scenario where the same resource is available in two repositories, at the organization level and at the environment level:

https://api.enterprise.apigee.com/v1/organizations/myorg/resourcefiles/jsc/pathSetter
https://api.enterprise.apigee.com/v1/organizations/myorg/environments/prod/resourcefiles/jsc/pathSetter

Now imagine that an API proxy is configured with the following Policy:

<Javascript name='PathSetterPolicy' timeLimit='200'>
    <ResourceURL>jsc://pathSetter.js</ResourceURL>
</Javascript>

The policy reference cannot explicitly resolve to a repository. The first resource whose name matches the resource name in the policy is resolved.

So, when the API proxy is deployed in the environment prod, the Policy will resolve the pathSetter.js resource:

/organizations/myorg/environment/prod/resourcefiles/jsc/pathSetter

When deployed in the environment test, the Policy will resolve:

/organizations/myorg/resourcefiles/jsc/pathSetter

Retrieving resources

You can view a resource body by calling the GET method on it:

$ curl -X GET -H "Accept:application/json"
https://api.enterprise.apigee.com/v1/organizations/myorg/resourcefiles/jsc/pathSetter" \
-u myname:mypass

Sample response:

request.setHeader("RequestPath", flow.getVariable("proxy.basepath"));

Add new comment

Provide your email address if you wish to be contacted offline about your comment.
We will not display your email address as part of your comment.

We'd love your feedback and perspective! Please be as specific as possible.
Type the characters you see in this picture. (verify using audio)

Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.