Getting started with tasks
Create a task
We'll be creating a task that fetches data from a trivia API and presents a trivia question and answer.
First, let's create a task from the UI. From your home page, click
Newto reach the New Task page and select
Define your task
Give your task a name (e.g. "Get Trivia Question").
For now, you can leave the description and parameters empty.
Continueto skip through the Build and Advanced sections for now. At the last section, click
Create taskto be taken to your new task's page:
Deploying task code
If you haven't yet, first install the Airplane CLI (
airplane) by following the installation instructions. We'll be using
airplaneto deploy from the terminal.
Once you've installed and logged in to the CLI, you can run the
initcommand shown on the task page:
bashCopied1# airplane init --slug [slug] path/to/new_file.js2airplane init --slug get_trivia_question ./get_trivia_question.js
You can choose any path you want for where the script is stored. To use TypeScript, use a
.tsextension instead of
This will create a script that looks something like this:
Airplane looks for the default export function and calls it. The first argument will contain the parameters for the run—more on that later.
As noted by the comment, you're welcome to change the script but leave the first line intact. The Airplane CLI uses that line to identify which task your code corresponds to.
Now you can deploy the task:
bashCopied1airplane deploy ./get_trivia_question.js
This will bundle up the source code, build it, and deploy it to Airplane.
Go back to the task page (or reload it), and you should now be able to execute it!
The task should succeed (but you won't see any useful output):
Adding NPM dependencies and output
Next, let's have this task actually do something. We'll make an HTTP request to http://jservice.io/api/random?count=5 to retrieve (e.g.) 5 trivia questions.
If you haven't yet, let's create an NPM package:
bashCopied1# This creates a package.json file. Make sure you're not in a directory2# that already has one!3npm init4# If you want to skip all the prompts:5# npm init -y
Next, let's install the
airplaneSDK as well as
bashCopied1npm install airplane node-fetch
This should have updated your `package.json` with `airplane` and `node-fetch`. The Airplane CLI looks for `package.json` when deploying and will use it to install dependencies. Both NPM and Yarn lock files are supported.
Update your script with the following code:
airplane deploy ./get_trivia_question.js
Now try executing the task again, and you should see a table of questions!
Here, we've used
airplane.appendOutputfrom the SDK to hook into the Outputs feature of Airplane. This allows us to log both structured and unstructured data from the task and surface that to the user. When objects are outputted, Airplane automatically collects the object into a table with object keys as columns.
You can filter/search in a table directly from Airplane as well as export to CSV:
Next, let's add a parameter so that we can configure how many trivia questions are returned.
Edit taskfrom the task page:
Add Parameter. Give it a name, "Number of Questions", and select
Integerfor the Type. Click
Update taskto save.
Note the text saying
Slug is number_of_questions. This is the
parameter slugand serves as an identifier you'll need from your code.
Let's use the parameter from our code. Airplane automatically passes parameters in as the first parameter to your function. We'll find our parameter at
airplane deploy ./get_trivia_question.js.
You'll now be able to enter
Number of Questions:
And you'll see that the output table now only has 3 rows:
Config Variables provide a way to store configuration and secrets securely on Airplane. Config variables can be set via the UI and then read in as environment variables on the task.
In a more realistic example, you would likely store your `database_url` as a config variable and access it from your code to connect to your DB.
First, click the
Config variablespage from the sidebar:
New config variableand in this example, we'll store the API URL as a config variable pointing to
Return to your task and click
Edit task. Under the
Buildsection, add an environment variable called
TRIVIA_API_URLand set it to the config variable you just added:
Don't forget to click
Update taskwhen you're done.
Now, let's read that environment variable in our code, via
That's it for now! In this quickstart, you were able to take a custom script, deploy it to Airplane, and configure both its parameters and outputs.