Overview
Defines a connection to an external REST API using OAuth2 client credentials flow.
Connection File: connections/external-api.json
JSON
{
"name": "externalApi",
"type": "rest-oauth2",
"url": "https://{{api.host}}",
"authUrl": "https://{{api.authHost}}/oauth/token",
"clientId": "{{api.clientId}}",
"clientSecret": "{{api.clientSecret}}",
"authRequestTemplate": "{\"grant_type\": \"client_credentials\", \"client_id\": \"::clientId\", \"client_secret\": \"::clientSecret\"}"
}
Properties:
api.host=api.example.com
api.authHost=login.example.com
api.clientId=my-client-id
api.clientSecret=my-client-secret
Using the Connection in Routes
XML
<routes xmlns="http://camel.apache.org/schema/spring">
<route id="fetchFromExternalApi">
<from uri="timer://trigger?repeatCount=1"/>
<to uri="pfx-rest:get?uri=/v1/products&connection=externalApi"/>
<to uri="pfx-api:loaddata?objectType=P&mapper=productMapper"/>
</route>
</routes>
How It Works
-
On first request, IM calls
authUrlwith theauthRequestTemplateto obtain an access token -
The token is cached and reused until it expires
-
On expiry, IM automatically refreshes the token — no manual handling required
Common Pitfalls
-
authRequestTemplateuses::clientIdand::clientSecretas placeholders (double colon, not{{}}) -
The
urlis the base URL for API calls;authUrlis only for token exchange -
Different APIs may require different
grant_typevalues — adjustauthRequestTemplateaccordingly