APIthon Reference

View as Markdown

How APIthon Works

The most important thing to remember about APIthon is that it returns the result from the last line of code

Let’s look at some examples:

1cookies = 5
2more_cookies = 3
3cookies + more_cookies

This will give us back 8, because adding cookies is the last thing we did! But if we write:

1cookies = 5
2cookies + 3
3more_cookies = 2

We won’t get anything back, because the last thing we did was just make a new variable!

Python vs. APIthon

Capabilities

  1. No Imports
    • You cannot import any external modules or packages
    • You can only use the built-in functions that are available
  2. No Classes
    • You cannot define new classes
    • You must work with existing data types
  3. No Private Methods or Properties
    • Anything that starts with an underscore (_) is off-limits
    • This includes both methods and variables
  4. No External Code
    • All code must be self-contained
    • No accessing external resources
  5. No Multiple Language Support
    • APIthon only works with Python-style syntax
    • No mixing with other programming languages

Size Limits

APIthon also has some size limits:

  • Your code can’t be too long (4096 bytes)
  • Your lists can’t have too many things in them (2096 bytes)
  • Your numbers can’t be too big or too small (4294967296)
  • Your words (strings) can’t be too long (4096 bytes/characters)

Using APIthon

Common Operations

APIthon supports many common operations across different data types. Here are the most useful ones you’ll likely use:

Working with Numbers (Integers and Floats)

1# Basic math operations
2number = 5
3other = 3
4number + other # Addition
5number - other # Subtraction
6number * other # Multiplication
7number / other # Division
8number // other # Floor division
9number % other # Modulus (remainder)
10number ** other # Power# Comparison
11number > other # Greater than
12number < other # Less than
13number >= other # Greater than or equal
14number <= other # Less than or equal
15number == other # Equal to

Working with Text (Strings)

1text = "Hello"
2other = "World"
3
4# String operations
5text + other # Joining strings
6text * 3 # Repeat string
7text.split() # Split into list
8text.join(['a','b']) # Join list into string
9text.replace('l', 'w') # Replace characters

Working with Lists

1my_list = [1, 2, 3]
2
3# List operations
4my_list.append(4) # Add item to end
5my_list.pop() # Remove and return last item
6my_list.remove(2) # Remove specific item
7my_list.index(1) # Find position of item
8my_list.sort() # Sort the list
9my_list.reverse() # Reverse the list
10len(my_list) # Get length of list

Working with Sets

1set_a = {1, 2, 3}
2set_b = {3, 4, 5}
3
4# Set operations
5set_a.add(4) # Add item
6set_a.remove(2) # Remove item
7set_a & set_b # Intersection
8set_a | set_b # Union
9set_a - set_b # Difference

Working with Dictionaries

1my_dict = {"name": "Alice"}
2
3# Dictionary operations
4my_dict["age"] = 20 # Add/update item
5my_dict.get("name") # Get value safely
6my_dict.keys() # Get all keys
7my_dict.values() # Get all values

Common Built-in Functions

1len(something) # Get length
2str(42) # Convert to string
3int("42") # Convert to integer
4float("42.5") # Convert to float
5bool(1) # Convert to boolean

Remember these important points about functions in APIthon:

  • All functions return values (just like in a Python shell)
  • The last operation’s result is what gets returned
  • There are limits on how many operations you can perform
  • If a function name starts with underscore (_), you can’t use it

APIthon Examples

Example 1: Joining a list of strings

1script:
2 output_key: uppercase_names
3 input_args:
4 names: data.user_list.names
5 code: "','.join(names)"

This takes a list of names and join them together into a single string!

Sample Input Payload:

1data.user_list.names = ["Alice", "Bob", "Charlie"]

Result:

1Alice,Bob,Charlie

Example 2: Calculating Total Points

1script:
2 output_key: total_score
3 input_args:
4 scores: data.game_results.points
5 code: "total = 0; [total := total + score for score in scores if score > 0]; total"

This adds up all the positive scores in a list!

Sample Input Payload:

1data.game_results.points = [10, -5, 20, 0, 15]

Result:

145

Example 3: Organizing a Pet Directory

1script:
2 output_key: pet_summary
3 input_args:
4 pets: data.pet_store.animals
5 code: |
6 pet_types = {}
7
8 for pet in pets:
9 pet_type = pet.get('type')
10 if pet_type not in pet_types:
11 pet_types[pet_type] = []
12 pet_types[pet_type].append(pet.get('name'))
13
14 summary = ""
15 for pet_type in pet_types:
16 names = pet_types[pet_type]
17 summary += f"We have {len(names)} {pet_type}(s):\n"
18 for name in names:
19 summary += f"- {name}\n"
20 summary += "\n"
21
22 summary

This organizes a list of pets by type and creates a nice summary!

Sample Input Payload:

1data.pet_store.animals = [
2 {"type": "dog", "name": "Buddy"},
3 {"type": "cat", "name": "Whiskers"},
4 {"type": "dog", "name": "Max"},
5 {"type": "fish", "name": "Nemo"}
6]

Result:

We have 2 dog(s):
- Buddy
- Max
We have 1 cat(s):
- Whiskers
We have 1 fish(s):
- Nemo

Example 4: Grade Calculator

1script:
2 output_key: grade_report
3 input_args:
4 grades: data.student_records.scores
5 code: |
6 def calculate_grade(score):
7 if score >= 90: return 'A'
8 if score >= 80: return 'B'
9 if score >= 70: return 'C'
10 if score >= 60: return 'D'
11 return 'F'
12
13 total = 0
14 grade_counts = {'A': 0, 'B': 0, 'C': 0, 'D': 0, 'F': 0}
15
16 for score in grades:
17 total += score
18 grade = calculate_grade(score)
19 grade_counts[grade] += 1
20
21 average = total / len(grades)
22
23 report = f"Class Average: {average:.2f}\n\nGrade Distribution:\n"
24 for grade, count in grade_counts.items():
25 report += f"Grade {grade}: {count} students\n"
26
27 report

This takes a list of grades, calculates statistics, and creates a detailed report!

Sample Input Payload:

1data.student_records.scores = [95, 82, 74, 65, 88, 90, 55, 89, 92, 78]

Result:

Class Average: 80.80
Grade Distribution:
Grade A: 3 students
Grade B: 4 students
Grade C: 1 students
Grade D: 1 students
Grade F: 1 students

Troubleshooting

  • Remember to use | after code: to tell the editor that you’re about to write multiline code
  • Make sure your indentation is correct - Python is very picky about this!
  • The last line of your code is what gets saved to your output_key
  • Be aware of the size limits when working with collections