Skip to main content

5. Adding Functions

The idea behind functions in API Parrot is to be able to break out specific parts of a collection to serve different purposes. This can be useful if you have a large collection with requests that does multiple things, and you want to separate these things into smaller parts and make it more modular. Functions also differ from collections in that they can have user-defined inputs and outputs.

To start with, we will create a function Get City And State From Zip that takes a ZIP code as input and returns the city and state for that ZIP code. This function will be used to extract the city and state from the response of the USPS ZIP Code Lookup.

Creating a Function

To create a function for a collection, click the + New Function button in the left sidebar. This will create a new function and open a new tab for that function.

Create Function

Renaming a Function

Renaming a function is done in the same way as renaming a collection. Right-click the function in the left sidebar and select Rename or select the function and press F2. This will make the function name editable.

Rename Function

Note on Collection to Function inheritance

When creating a function from a collection, the function will inherit all the variables and exchanges from the collection. When you delete a exchange in the parent collection, it will also be deleted in the function. However, when you disable an exchange or variable in the parent collection, it will look like it is deleted in the function.

In our example the disabled variables from the parent collection USPS Search Zip are not at all visable in the function Get City And State From Zip.

The idea is that you clean up the collection from stuff that is not necessary for any of the functions by deleting or disabling items in the collection, which will make the child functions cleaner and easier to work with.