Creating flows for an API (Part 1)

A flow for an API contains a request, one or more target application actions, and a response for an API operation. The flow is triggered by calling the API operation such as from mobile and web applications.

If you want a developer to be able to create an app that uses the data in your applications, you can provide an API. For example, you might provide an API that can create, retrieve, and update customers in your CRM system. To define the API, you complete the following high-level steps.
  1. You create one or more models that define the structure of the objects that you want to create or retrieve.
  2. You choose the create, retrieve, or update operations to complete against each model.
  3. You configure a flow that implements each operation, adding one or more target applications that hold the data.

Watch the video that demonstrates this process. A video that demonstrates how to create flows for an API.

The following steps describe in detail how to create a flow for an API in App Connect to create a Salesforce contact.

Find or create everything you need

  • If you're not already connected to your Salesforce account, find the name and password of your Salesforce account.
    Note: To create a free test account in Salesforce rather than use your business account, register for a Developer account from the URL https://developer.salesforce.com instead of a Trial account. If you connect to App Connect with a Trial account, the Salesforce events do not work.

Create the flow for the API

  1. On the Dashboard, click Create > Flows for an API.
  2. Enter a name that identifies the purpose of your API (for example, Customer API).
    Figure 1. Enter a name that identifies your API
    Enter a name that identifies your API
  3. In the Model name field, enter a name for your model that reflects the type of object that your API works with.

    The API flow that you're going to build creates a customer in Salesforce. Therefore, in the Model name field, type Customer.

  4. Click Create model. The Customer model panel is displayed. This panel contains a Properties tab and an Operations tab.

    You first set properties on the Properties tab to define the structure of the object that the API works with.

    1. You want the API to create, update, and retrieve a customer. Therefore, use the following properties to identify customers.
      CustomerID
      FirstName
      LastName
      Email

      Add these properties to the Customer model. (To add the first property, type the name in the field provided. To add another property to the list, click Add property (+)).

      Figure 2. Adding properties for the API.
      Adding properties for the API.
      A valid property name has the following characteristics:
      • Each property name must be unique.
      • A property name can't contain spaces, but you can use an underscore character (_) to separate words.
      • It must contain only letters, numbers, or the underscore character.
      • It must contain at least 2 characters.
      • It must begin with a letter or an underscore.
      Tip: Either type in the name of a property or click Select properties from applications to choose properties from one or more of the applications that you're connected to.
    2. By default, the first property that you add (CustomerID) is selected as the ID property. Because you want your flow to return a customer ID when you create the customer object, leave this property as the ID property.
      Note: Your flow must return the ID property when it creates an object. Or the ID property must be sent in a request to update or retrieve an object by using its ID. You can set only one property as the ID property.

      You can create more models by clicking Create model. To delete a model, select Delete model from the menu.

  5. To define how the API interacts with the object, click Operations. You can add operations to achieve the following actions.
    • Create an object.
    • Retrieve an object by using its unique ID or by using a filter.
    • Update or create an object by using its ID or a filter. If the object exists, update it. If the object doesn't exist, create it.
    1. Add the Create Customer operation to the Customer model. (Click in the Select an operation to add list, then select Create Customer.)
      Figure 3. Adding the Create Customer operation
      Selecting the Create Customer operation
      Note: You can define your own operations, by clicking Add a custom operation. Note the following restrictions if you decide to use this option:
      • The operation name can't be a keyword.
      • The operation name can't be one of the following keywords: create, updateOrCreate, all, updateAttributes, update, updateAll, upsertWithWhere, replaceOrCreate, replaceById, destroy, destroyAll, executeAssembly.
      • The query parameter can't be the same as the model ID.

      For more information, see Create your own custom HTTP operations on API flows.

      Next, create a flow that defines how the Create Customer operation works.

  6. Click Implement flow. You see a basic flow in the flow editor with a Request node, a Response node, and an add node icon Icon to add a node to a flow to add one or more target applications. Notice the structure of the Request body example, which is constructed from the properties in your model with some sample data.
    Figure 4. An example of a request body from the Customer model with some sample data
    Request body example from the Customer model, with some sample data
  7. To add the target application to the flow, click (+).
  8. Select Salesforce > Contacts > Create contact.

    (If App Connect isn't already connected to Salesforce, specify the name and password of your Salesforce account.)

  9. Map the fields from the request into the Salesforce Create contact action as follows.
    • Click in Account ID and click Insert a mapping Insert a mapping icon. Then select CustomerID from the Available inputs list.
    • In Last Name, type last, then select LastName from the list.
    • In First Name, type fir, then select FirstName from the list.
    • In Email, type em, then select Email from the list.
    Figure 5. Map fields from the request into the Salesforce Create contact action
    Mapped fields for the Salesforce action
  10. You can also add some conditional logic to your flow. For example, if you want your flow to do different things for different conditions, you can add one or more ‘If’ nodes to your flow. For more information, see Adding conditional logic to a flow.
  11. Click the Response node in the flow to define the response that is returned when the operation completes successfully. Map the available fields from the Salesforce Create contact action to the response as follows.
    • For the mandatory response field CustomerID, map the Salesforce field Contact ID. If needed, map other fields to return their values in the response.
      Figure 6. Map the available fields from the Salesforce Create contact action to the response
      API response node mappings
      Note: In the Response header section, you can choose your own response code mapping. The following response codes are returned for the different operations:
      • Create operations return a response code of 201 (record created).
      • Retrieve operations return a response code of 200 (record retrieved).
      • Replace or create operations return a response code of 200 (record replaced) or 201 (record created).
  12. Click Done to return to your model.
  13. From the options menu (⋮), click Start API.
    Figure 7. Start the API from the options menu
    Start API in the options menu

Test the flow for your API

You can test the flow for your API in the API portal, or by using your preferred method to invoke an API, such as by using cURL.

Testing in the API portal

  1. On the Manage tab, go to the Sharing Outside of Cloud Foundry organization section.
  2. Click Create API key and documentation link. Notice that a generated API key is automatically provided in the Create API key field. You can use this key or specify your own custom key.
  3. Give the key a descriptive name; for example: Customer API test, and then click Create.
    API key for testing flow for API in API portal.
  4. Copy the API Documentation Link link and paste into your web browser to open the API in an API portal.
  5. Click the API operation, for example Post / contacts, to show the Details tab for the API operation.
  6. To invoke your API in the API portal, click the Try it tab.
  7. In the Security / Client ID field, paste the API key.
  8. Scroll down to the "Parameters" section, and then click Generate to generate some sample customer data in the data field. You see sample data similar to the following example.
    
    {
    "CustomerID": "2766950149128192",
    "FirstName": "Test1",
    "LastName": "APIname",
    "Email": "test1.apiname@email.com"
    }
    
  9. Delete the CustomerID line from the generated data because an ID doesn't need to be provided when you create a contact in Salesforce. (The CustomerID line was included in the generated data because it was defined as one of the properties for the Customer model. Salesforce generates and returns this value when a contact is created.)
  10. Click Send.
  11. Scroll down to the response section, where you can see the success code (201 Created) and the response body that contains the CustomerID value that Salesforce assigns.
    
    Response
    Code: 201 Created
    Headers:
    ...
    
    {
      "CustomerID": "0034I00001pvaenQAA"
    }
    
  12. You created and tested your API. Close the API portal tab to return to the API flow. Then, click Dashboard to return to the App Connect Dashboard.

    In the Dashboard, you can see the Customer API tile for your API flow. You can start and stop your API flow in the same way as any other flow. You can also open the API flow while it's running, but you must stop it before you can edit it.

    Tile for the Customer API flow
  13. Go to your Salesforce instance, and open Contacts. A new contact for your sample data was created.

    To find out how to retrieve and update Salesforce contacts by using the same API, see Creating flows for an API (Part 2).

Conclusion

You created an API. In flow tiles on the App Connect Dashboard, the API icon identifies flows for APIs. You can start and stop them in the same way as any other flow. You can open an API while it's running, but you must stop it before you can edit it.

Figure 8. The flow tile on the App Connect Dashboard
The tile for the running API flow.

To find out how to retrieve and update Salesforce contacts by using the same API, see Creating flows for an API (Part 2).