A maze generator is easy to write badly. Carve some passages, knock down some walls, ship it — and sooner or later it hands you a maze where the goal is walled off completely. In a game that's an unwinnable level. In a printed activity book, that's a kid discovering the answer key doesn't work, and a refund instead of a review.

So the maze generator I built for my books doesn't just carve — it proves. It walks the finished maze from start to goal before anything ships, and the proof travels with the puzzle. This is that generator, exposed as an API called ActivityForge. In this tutorial we'll call it from Python: send a size and a seed, get back a maze, its wall data, and the verified solution path.

What we're building

A small Python script that:

  1. POSTs to the maze endpoint with a difficulty (medium) and a seed (42)
  2. Renders the maze as ASCII art from the wall data in the response
  3. Prints the solution — steps, directions, and the route drawn over the maze — plus saves the print-ready PDF

The API lives on RapidAPI. The free tier gives you 100 calls a day, and every plan — free included — reaches every endpoint and every format. The paid plans only change how many calls you get, starting at $19/month. Nothing in this tutorial needs a paid plan.

Setup

Subscribe on the ActivityForge listing (the free tier is fine), copy your key from the listing's Endpoints page, then:

pip install requests export RAPIDAPI_KEY=your_key_here 

Step 1: Make the call

The whole request is one POST. Two headers carry your key and the host; the JSON body carries the maze settings.

import os import sys import requests HOST = "activityforge.p.rapidapi.com" def generate_maze() -> dict: response = requests.post( f"https://{HOST}/v1/maze", headers={ "X-RapidAPI-Key": os.environ["RAPIDAPI_KEY"], "X-RapidAPI-Host": HOST, "Content-Type": "application/json", }, json={ "difficulty": "medium", # "easy" | "medium" | "hard" "seed": 42, # same seed = same maze, forever "format": "all", # "json" | "svg" | "pdf" | "all" }, timeout=30, ) if not response.ok: try: message = response.json()["error"]["message"] except Exception: message = "request failed" sys.exit(f"ActivityForge {response.status_code}: {message}") return response.json() 

A few things worth knowing about the parameters:

  • difficulty — easy is 6×6, medium 10×10, hard 14×14. Want an exact size? Send cols and/or rows (4 to 30 each) — they override the preset.
  • include_solution — defaults to true. Send false to keep the answer key out of the response — say, when the maze goes straight to a player and the solution stays on your server.
  • format — json returns just the maze data, svg or pdf add a rendered page, and all returns everything in one response. Errors come back in a consistent shape — {"error": {"code", "message"}} — which is what the error handling above reads.

Step 2: Render the maze from the wall data

The response describes the maze as data, not a picture. Coordinates are [x, y] — x is the column, y is the row — with start at the top-left cell [0, 0] and goal at the bottom-right. The walls field is a list of rows; each cell carries four booleans, top, right, bottom, and left, saying which of its walls stand. That's everything a renderer needs:

def render_maze(maze: dict, marked: set | None = None) -> str: walls, cols, rows = maze["walls"], maze["cols"], maze["rows"] start, goal = tuple(maze["start"]), tuple(maze["goal"]) marked = marked or set() lines = [] for y in range(rows): top = "".join("+" + ("---" if walls[y][x]["top"] else " ") for x in range(cols)) lines.append(top + "+") row = "" for x in range(cols): row += "|" if walls[y][x]["left"] else " " if (x, y) == start: row += " S " elif (x, y) == goal: row += " G " elif (x, y) in marked: row += " * " else: row += " " row += "|" if walls[y][cols - 1]["right"] else " " lines.append(row) bottom = "".join("+" + ("---" if walls[rows - 1][x]["bottom"] else " ") for x in range(cols)) lines.append(bottom + "+") return "\n".join(lines) def main() -> None: if "RAPIDAPI_KEY" not in os.environ: sys.exit("Set RAPIDAPI_KEY first.") maze = generate_maze() print(f"Maze — difficulty: {maze['difficulty']}, " f"{maze['cols']}x{maze['rows']}, seed {maze['seed']}\n") print(render_maze(maze)) 

Run it and you get:

Maze — difficulty: medium, 10x10, seed 42 +---+---+---+---+---+---+---+---+---+---+ | S | | | +---+---+ +---+---+ +---+ +---+ + | | | | | +---+ +---+---+---+---+ +---+---+ + | | | | + + + +---+ + +---+---+---+---+ | | | | | | + + +---+---+ +---+ +---+ + + | | | | | | | | + +---+ + +---+---+ + +---+---+ | | | | | + + + +---+---+---+---+ +---+ + | | | | | | + + +---+---+ +---+---+---+ + + | | | | | | + +---+ +---+---+---+ +---+ + + | | | | | | | + + +---+---+ +---+---+ +---+ + | | G | +---+---+---+---+---+---+---+---+---+---+ 

That's a real response, not a mockup — seed 42 will hand you this exact maze every time.

Step 3: The solution is already in the response

Here's the part that makes the maze safe to ship. Alongside the walls, the response carries a solution object: path (the route as [x, y] cells, start to goal), directions (the same route as words — right, down, left, up), and steps (the move count).

The generator builds a perfect maze — a spanning tree over the grid, so exactly one route joins any two cells — then breadth-first walks that route before returning anything. The solution is the route it walked, so it can't disagree with the walls. Feed the path back into the renderer as marked cells and the proof draws itself:

 solution = maze["solution"] print(f"Solution: {solution['steps']} steps") print("Directions:", ", ".join(solution["directions"])) marked = {tuple(cell) for cell in solution["path"]} print(render_maze(maze, marked)) 
Solution: 38 steps Directions: right, right, down, right, right, right, up, right, right, down, right, right, down, left, left, left, left, down, right, down, down, left, left, left, up, left, down, down, right, right, down, right, right, down, right, down, right, right +---+---+---+---+---+---+---+---+---+---+ | S * * | * * * | | +---+---+ +---+---+ +---+ +---+ + | | * * * * | | * * * | +---+ +---+---+---+---+ +---+---+ + | | | * * * * * | + + + +---+ + +---+---+---+---+ | | | | * * | | + + +---+---+ +---+ +---+ + + | | | * * | | * | | | + +---+ + +---+---+ + +---+---+ | | * | * * * * | | + + + +---+---+---+---+ +---+ + | | | * * * | | | + + +---+---+ +---+---+---+ + + | | | * * * | | | + +---+ +---+---+---+ +---+ + + | | | | * * | | | + + +---+---+ +---+---+ +---+ + | | * * G | +---+---+---+---+---+---+---+---+---+---+ 

Trace the stars yourself: every step moves through a gap, never through a wall, from the S in the top-left to the G in the bottom-right. Generating levels? That directions list doubles as a walkthrough your game can replay. Printing? It's the answer key.

Step 4: Save the printable PDF

Because we asked for format: "all", the same response also carries svg (a print-ready page) and pdf_base64 (a vector PDF, base64-encoded). Saving the PDF is three lines:

import base64 with open("maze.pdf", "wb") as f: f.write(base64.b64decode(maze["pdf_base64"])) 

That's the page you hand to a kid — titled, with "Find your way from START to GOAL!" on it, no drawing required.

The seed trick

Look back at the request: "seed": 42. Run it tomorrow, next month, on a different machine — same maze, wall for wall, same 38-step solution. Leave the seed out and every call returns a fresh random maze. For print: generate a catalog once, record the seeds, re-render identical puzzles forever. For a game: a daily maze that's identical for every player, from one integer.

What else it does

The maze is one of four generators, all with the same request shape and the same seed behavior:

  • POST /v1/word-search — themed word searches (or your own word list) with every word's placement recorded as the answer key.
  • POST /v1/dot-to-dot — 8 shapes (star, heart, flower, balloon, house, rocket, fish, tree), 10–60 dots, numbered in order.
  • POST /v1/activity-page — a themed, age-banded page set that bundles the activities together with answer-key pages, ready to print on US Letter.

Every generator proves its own output the same way — word placements re-checked against the finished grid, dot sequences checked complete and ordered — because a broken puzzle in a printed book is a refund, not a bug report.

Try it

The free tier is 100 calls a day, which is plenty to build something real. The word-search script from the previous tutorial, plus a JavaScript version, is in the examples repo, and the API itself is on RapidAPI.

If you generate a maze pack or a level set with it, I'd like to see what you made.

Part of the ActivityForge series — the previous tutorial builds a printable word search with its answer key, and the launch post covers how these generators came out of my activity-book pipeline.