Contact Us 1-800-596-4880

Implement the American Flights API

After designing and publishing your API specification, you’re ready to implement it in Anypoint Code Builder. At this stage of the tutorial, you already have an API specification for the American Flights API published to Anypoint Exchange, and you’re working in the same workspace where you build the integration.

To implement the API, you scaffold the API specification into an integration project. Scaffolding generates the API interface based on the specification, creating the flows and configuration that you then extend with application logic.

Scaffold the American Flights API into an Integration Project

Now that the American Flights API specification is available in Anypoint Exchange, scaffold it into a new integration project to generate the API interface.

Use MuleSoft Vibes to scaffold the locally generated API Spec by sending the following message:

Scaffold the american-flights-api.yaml API spec.

Alternatively, you can manually implement your published API spec from Exchange:

  1. In the activity bar, click the (Anypoint Code Builder) icon.

  2. From Create, click Implement an API.

    The Anypoint Code Builder panel with the Create and Import options, including Implement an API
  3. In the Implement an API Specification tab that opens:

    1. Provide a new name for the implementation project.

    2. Select or create a new project location.

    3. In the search box, type American Flights API, or the name you used when creating the specification, and press Enter.

    4. Find your API specification on Anypoint Exchange, and click Add Asset to insert the API name into the field.

    5. Select an existing workspace, or create a new one by providing a name and a location.

    6. Click Create Project to start scaffolding the specification into a new implementation project.

    7. If you see this notification, trust your workspace by clicking Manage then Trust.

      The workspace trust notification with Manage and Trust options

After scaffolding completes, confirm that the generated project contains listener configuration, APIKit router flows, flows associated with API methods, and error handlers.

Flow list showing the scaffolded american-flights-api-main flow with a Listener and Router, alongside error handler flows

Connect to a Database Using the Database Connector

Add and configure a Database Select operation to retrieve flight data from a database.

  1. Open the get:\flights:american-flights-api-config flow.

    If you don’t see the flow list, click Show flow list to display it.

    The Show flow list button in Anypoint Code Builder used to display the list of flows in the canvas
  2. Click Add Component (+) before the existing Transform Message and select Connectors > Database > Select.

    If there’s no Transform Message, add the Database - Select component at the beginning of the flow. You add or configure the Transform Message component later in this section.

  3. Click the Select component to open its properties panel.

  4. Click Edit name (The Edit Name icon.), change it to All Flights, then click Apply (The Apply icon.).

  5. In the Select component properties, click Add New Connection next to Connection Config.

    Database - Select properties panel with the Add New Connection link next to Connection Config
  6. Set the connection to MySQL Connection.

    Connection Config dialog with Name set to Config and Connection set to MySQL Connection
  7. Click Configure library next to MySQL JDBC Driver.

    Required Libraries section with the Configure library option next to MySQL JDBC Driver
  8. In the Select Library Source drop-down menu, select Maven Dependency.

    Select Library Source drop-down menu set to Maven Dependency
  9. Enter mysql: in the Search Maven Central Repository text field, and select mysql:mysql-connector-java in the results.

    Search Maven Central Repository results showing mysql:mysql-connector-java and other matches
  10. Enter 8.0.30 in the Version text field, and click Apply.

  11. Confirm that the MySQL JDBC Driver is set.

    Required Libraries section confirming MySQL JDBC Driver is set to mysql:mysql-connector-java:8.0.30
  12. Set the following configuration values:

    Field Name Field Value

    Host

    iltdb.mule-training.com

    Port

    3306

    User

    mule

    Password

    mule

    Database

    training

    Connection Config dialog with Host, Port, User, Password, and Database fields set
  13. Click Test Connection and confirm that you see a Connection is valid message.

    Connection is valid confirmation message
    If the test fails, verify the connection details and retry. If you are using a VPN, confirm that it is not preventing the connection to the MySQL database.
  14. Click Apply and close the Database - Select config tab.

  15. In the Database - Select properties panel, add this query in the SQL Query Text field:

    SELECT * FROM american
    Database - Select properties panel with the General tab showing the SQL Query Text field set to SELECT * FROM american

    This query selects all records from the american table.

  16. Locate the Transform Message component after the All Flights Database Select.

    If there’s no Transform Message component there, click Add Component (+) after All Flights and select Core Processors > Components > Transform Message.

    The get:\flights:american-flights-api-config flow with an All Flights Database Select component followed by a Transform Message component
  17. Click the Transform Message component to open its properties panel.

  18. In the Inline Script field, set the script to:

    %dw 2.0
    output application/json
    ---
    payload