Customize API Life Cycle
APIs created in WSO2 API Manager have their own life cycles. Each life cycle has a set of states, specific actions for each state transition, and a checklist of items before a state transition occurs. An API has a default life cycle with six states, but you can customize it.
This tutorial demonstrates how to edit the default API lifecycle by adding a new state.
Capability to customize the default API life cycle is not available in API Manager versions prior to 1.10.0.
Follow the steps below to add a new state to the default APIÂ life cycle.
- Log into the API Publisher and select an API you have previously created.Â
- Click Lifecycle to view the current states available by default.
- Open the API Manager's Management Console:Â https://localhost:9443/carbon.
- Go to Extensions > Configure > Lifecycles.
- Click the View/Edit link corresponding to the default API life cycle.
Note the API life cycle configurations. Here's an example:
<aspect name="APILifeCycle" class="org.wso2.carbon.governance.registry.extensions.aspects.DefaultLifeCycle"> <configuration type="literal"> <lifecycle> <scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0" initialstate="Created"> <state id="Created"> <datamodel> <data name="checkItems"> <item name="Deprecate old versions after publish the API" forEvent=""> </item> <item name="Require re-subscription when publish the API" forEvent=""> </item> </data> <data name="transitionExecution"> <execution forEvent="Deploy as a Prototype" class="org.wso2.carbon.apimgt.impl.executors.APIExecutor"> </execution> <execution forEvent="Publish" class="org.wso2.carbon.apimgt.impl.executors.APIExecutor"> </execution> </data> </datamodel> <transition event="Publish" target="Published"/> <transition event="Deploy as a Prototype" target="Prototyped"/> </state> <state id="Prototyped"> <datamodel> <data name="checkItems"> <item name="Deprecate old versions after publish the API" forEvent=""> </item> <item name="Require re-subscription when publish the API" forEvent=""> </item> </data> <data name="transitionExecution"> <execution forEvent="Publish" class="org.wso2.carbon.apimgt.impl.executors.APIExecutor"> </execution> <execution forEvent="Demote to Created" class="org.wso2.carbon.apimgt.impl.executors.APIExecutor"> </execution> </data> </datamodel> <transition event="Publish" target="Published"/> <transition event="Demote to Created" target="Created"/> <transition event="Deploy as a Prototype" target="Prototyped"/> </state> <state id="Published"> <datamodel> <data name="transitionExecution"> <execution forEvent="Block" class="org.wso2.carbon.apimgt.impl.executors.APIExecutor"> </execution> <execution forEvent="Deprecate" class="org.wso2.carbon.apimgt.impl.executors.APIExecutor"> </execution> <execution forEvent="Demote to Created" class="org.wso2.carbon.apimgt.impl.executors.APIExecutor"> </execution> <execution forEvent="Deploy as a Prototype" class="org.wso2.carbon.apimgt.impl.executors.APIExecutor"> </execution> </data> </datamodel> <transition event="Block" target="Blocked"/> <transition event="Deploy as a Prototype" target="Prototyped"/> <transition event="Demote to Created" target="Created"/> <transition event="Deprecate" target="Deprecated"/> <transition event="Publish" target="Published"/> </state> <state id="Blocked"> <datamodel> <data name="transitionExecution"> <execution forEvent="Re-Publish" class="org.wso2.carbon.apimgt.impl.executors.APIExecutor"> </execution> <execution forEvent="Deprecate" class="org.wso2.carbon.apimgt.impl.executors.APIExecutor"> </execution> </data> </datamodel> <transition event="Deprecate" target="Deprecated"/> <transition event="Re-Publish" target="Published"/> </state> <state id="Deprecated"> <datamodel> <data name="transitionExecution"> <execution forEvent="Retire" class="org.wso2.carbon.apimgt.impl.executors.APIExecutor"> </execution> </data> </datamodel> <transition event="Retire" target="Retired"/> </state> <state id="Retired"> </state> </scxml> </lifecycle> </configuration> </aspect>
Copy the following sample and paste it in the file. This adds a new state called
REJECTED
between thePUBLISHED
andRETIRED
states in the existing API life cycle. <state id="Rejected"> <datamodel> <data name="checkItems"> <item name="Deprecate old versions after rejecting the API" forEvent=""> </item> <item name="Remove subscriptions after rejection" forEvent=""> </item> </data> <data name="transitionExecution"> <execution forEvent="Re-Submit" class="org.wso2.carbon.apimgt.impl.executors.APIExecutor"> </execution> <execution forEvent="Retire" class="org.wso2.carbon.apimgt.impl.executors.APIExecutor"> </execution> </data> </datamodel> <transition event="Re-Submit" target="Published"/> <transition event="Retire" target="Retired"/> </state>
The sample REJECTED state uses the
Re-submit
andRetire
state-transition events to change to the consequent states. The custom checklist items are also given under"checkItems"
, which are tasks to be done in a state transition. You can select/deselect these items in the management console.For all state transitions, the same execution class is used (
org.wso2.carbon.apimgt.impl.executors.APIExecutor
).  However, you can plug your own execution code when modifying the life cycle configuration. For example, to add notifications for a specific state transition, plug your own custom execution class for that particular state in the API life cycle. You see the changes in the Lifecycle tab accordingly.Add a new transition event under the
PUBLISHED
state to show the state change toREJECTED
.... <transition event="Reject" target="Rejected"/> ...
- Go toÂ
<AM_HOME>/repository/deployment/server/jaggeryapps/publisher/site/conf/locales/jaggery/locale_default.json
 and aAddÂ"reject": "Reject"
 to make the transition event visible in the API Publisher. The key value in the JSON pair should be lowercase. - Re-open the API Publisher and check the Lifecycle to see the changes.
Tip: When you extend and customize the XML configuration of the API lifecycle:
- Â Do not change the life cycle name since it needs to be engaged with the APIs dynamically.
- Be sure to keep theÂ
PUBLISHED
andPROTOTYPED
 states as they are used by the API Publisher in the API creation wizard.
For more details on customizing the API lifecycle, see Extending the API Life Cycle.