Creating a sandbox environment in Apigee

4 1 3,036

Very often you need to create a sandbox for your API program. But maintaining the Sandbox at your datacenter can be really painful. You have to build it, maintain it, manage uptime etc. etc. A whole lot of work.

You can save yourself a lot of pain by creating a sandbox in Apigee.

Below is a Step by Step guide on how to do it.

Step 1:Create a Proxy with no-target

In this case I have created a basePath called /sandbox

Then add an AssignMessage Policy on the response flow. I have named it SendMockData

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="SendMockData">
    <DisplayName>SendMockData</DisplayName>
    <Set>
        <Payload contentType="application/json">
           \{
                "id": 1,
                "name": "A green door",
                "price": 12.50,
                "tags": ["home", "green"]
            }
        </Payload>
        <StatusCode>200</StatusCode>
        <ReasonPhrase>OK</ReasonPhrase>
    </Set>
</AssignMessage>

Deploy this and call this at {baseUrl}/sandbox.

Step 2:Now we want to simulate sending different content-type based on Accept headers:

Add a JsonToXML policy. Just the standard OOB JsonToXML will do and name it JSON-to-XML.

Now at the policy attachment point add a condition to check Accept header.So that your proxy/default.xml look like this:

<PreFlow name="PreFlow">
        <Request/>
        <Response>
            <Step>
                <Name>SendMockData</Name>
            </Step>
            <Step>
                <Name>JSON-to-XML</Name>
                <Condition>(request.header.Accept = "application/xml")</Condition>
            </Step>
        </Response>

Step 3:Now we add a new flow to create a new API which responds for a URL like sandbox/{id}

So create Flow with condition like

 <Flows>
        <Flow name="getById">
            <Description/>
            <Request/>
            <Response>
                <Step>
                    <Name>SendMockDataForId</Name>
                </Step>
            </Response>
            <Condition>(proxy.pathsuffix MatchesPath "/{Id}") and (request.verb = "GET")</Condition>
        </Flow>
    </Flows>

Then add another assignMessage policy to respond to the specific call and name it SendMockDataForId. And configure the payload in it.

Call it at {baseURL}/sandbox/10.

Step 4:Now we want to simulate an error response. Your sandbox sends an error response when Id is not 10.

Create an extract Variables policy on the request flow:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="Get-ID-from-URL">
    <DisplayName>Get ID from URL</DisplayName>
    <Properties/>
    <URIPath name="extractedId">
        <Pattern ignoreCase="true">/{id}</Pattern>
    </URIPath>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</ExtractVariables>

Then add an RaiseFault policy which looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RaiseFault async="false" continueOnError="false" enabled="true" name="Raise-Fault-if-Id-is-not-10">
    <DisplayName>Raise Fault if Id is not 10</DisplayName>
    <Properties/>
    <FaultResponse>
        <Set>
            <Headers/>
            <Payload contentType="application/json">
               \{"error": {"message":"Not Found", "detail":"The requested resource was not found."}}
            </Payload>
            <StatusCode>404</StatusCode>
            <ReasonPhrase>The Id is not available</ReasonPhrase>
        </Set>
    </FaultResponse>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</RaiseFault>

The RaiseFault policy should have a condition. So the flow should look like:

<Flows>
        <Flow name="getById">
            <Description/>
            <Request>
                <Step>
                    <Name>Get-ID-from-URL</Name>
                </Step>
                <Step>
                    <Name>Raise-Fault-if-Id-is-not-10</Name>
                    <Condition>(id !=10)</Condition>
                </Step>
            </Request>
            <Response>
                <Step>
                    <Name>SendMockDataForId</Name>
                </Step>
            </Response>
            <Condition>(proxy.pathsuffix MatchesPath "/{Id}") and (request.verb = "GET")</Condition>
        </Flow>
    </Flows>

Test it with {baseURL}/sandbox/10

and {baseURL/sandbox}/11

Similarly keep on adding more logic and to create your perfect SandBox.

There is one other huge option: Node.js. If you like to roll up your sleeves and code there is nothing like it. The above procedure is for a configuration approach.

Also all the variables are here hardcoded. You can store them in KVM or BaaS depending on the complexity of the data.

Comments
Not applicable

I want data to be sustained in sandbox for user level or environment level. how can we do that?

Version history
Last update:
‎01-25-2016 08:12 PM
Updated by: