Open Source API Gateway

Built-in Functions for Groovy, JavaScript and SpEL

Membrane API Gateway offers the same built-in functions in Groovy, JavaScript and SpEL expressions. Only the way to call them differs:

LanguageCall
Groovy (groovy, template)user() or fn.user()
JavaScript (javascript)fn.user()
SpEL (${...} in setHeader, if, log, ...)${user()}
- groovy:
src: |
header.put('X-User', user())
CONTINUE

- javascript:
src: |
header.put('X-User', fn.user());
'undefined'

- setHeader:
name: X-User
value: ${user()}
language: spel
- groovy:
    src: |
      header.put('X-User', user())
      CONTINUE
- javascript:
    src: |
      header.put('X-User', fn.user());
      'undefined'
- setHeader:
    name: X-User
    value: ${user()}
    language: spel

The variables like header, json and property are listed in Built-in Variables for Groovy, JavaScript and SpEL.

Message Content

FunctionReturnsDescription
isJSON()booleantrue if the current message has a JSON content type
isXML()booleantrue if the current message has an XML content type
jsonPath('$.items[0].price')ObjectEvaluates a JSONPath expression on the body of the message
xpath('//id')ObjectEvaluates an XPath expression on the body of the message
xpath('id', node)ObjectEvaluates an XPath expression relative to node
toJSON(object)StringSerializes a value, map or list to JSON
- if:
test: isJSON() and jsonPath('$.total') > 100
flow:
- setHeader:
name: X-Priority
value: high
- if:
    test: isJSON() and jsonPath('$.total') > 100
    flow:
      - setHeader:
          name: X-Priority
          value: high

Security

These functions read the result of an authentication plugin earlier in the flow, e.g. basicAuthentication, apiKey, jwtAuth or oauth2Resource.

FunctionReturnsDescription
user()StringName of the user authenticated by basicAuthentication
scopes()List<String>Scopes of the authenticated client
scopes('apiKey')List<String>Scopes from one security scheme: apiKey, jwt, oauth2, http
hasScope('read')booleantrue if the client has the scope
hasScope(['read','write'])booleantrue if the client has all of the scopes
hasScope()booleantrue if the client has any scope
isBearerAuthorization()booleantrue if the request has an Authorization: Bearer ... header
isLoggedIn('login')booleantrue if the session of the login plugin with that id is valid
getDefaultSessionLifetime('login')longSession lifetime of the login plugin with that id
- if:
test: not hasScope('admin')
flow:
- return:
status: 403
- if:
    test: not hasScope('admin')
    flow:
      - return:
          status: 403

Encoding

FunctionReturnsExample resultUse for
urlEncode('a b&c')Stringa+b%26cQuery parameter values
pathEncode('a b/c')Stringa%20b%2FcA single path segment
base64Encode('alice:secret')StringYWxpY2U6c2VjcmV0e.g. a Basic Authorization header
- setHeader:
name: Authorization
value: Basic ${base64Encode('alice:secret')}
- setHeader:
    name: Authorization
    value: Basic ${base64Encode('alice:secret')}

Other

FunctionReturnsDescription
env('HOME')StringValue of an environment variable, null if it is not set
weight(25.0)booleantrue in about 25 % of the calls, e.g. to split traffic
- if:
test: weight(10.0)
flow:
- setHeader:
name: X-Canary
value: "true"
- if:
    test: weight(10.0)
    flow:
      - setHeader:
          name: X-Canary
          value: "true"