apyhub
Back
▣ MODEL KIT

Run Graph Algorithms API

What it does

Graph Algorithms gives you traversal, path, and graph-structure utilities for directed and undirected graphs. Send a graph with vertices and edges, then use the specific algorithm endpoint to get the result back in a compact JSON shape.

Use breadth-first search or depth-first search when you need the visit order from a chosen start vertex. Breadth-first search returns { start, visited }, while Depth-first search returns the same response shape with a different traversal strategy. For directed graphs, both follow edge direction; for undirected graphs, edges are treated as bidirectional.

Use Topological sort on a directed graph when you need an ordering of vertices in a DAG. It returns { order }, which is useful for dependency resolution, build pipelines, task scheduling, and other workflows where prerequisites must come before dependents.

For weighted directed graphs, Shortest path returns either a single { start, end, reachable, distance, path } result when you supply end, or a { start, paths } map of distances and paths to all reachable vertices. Minimum spanning tree returns the selected edges and totalWeight for a spanning tree covering the graph, and Distance matrix returns a symmetric or asymmetric matrix depending on whether the graph is undirected or directed. These endpoints fit graph analysis, routing logic, and operations planning where you need explicit paths or pairwise distances.

▣ ENDPOINT 03 / 06
POST
Topological sort of the vertices in a directed, acyclic graph.
https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/topological-sort

QUICKSTART

GUIDE

Quickstart

This example sorts a small directed graph topologically.

curl -X POST "https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/topological-sort" \
  -H "apy-token: $APY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"graph":{"type":"directed","vertices":[{"id":"A"},{"id":"B"},{"id":"C"}],"edges":[{"f":"A","t":"B"},{"f":"B","t":"C"}]}}'

What you'll get back

Returns a JSON object with an order array of strings — the topological ordering of the graph's vertex IDs.

{
  "order": ["A", "B", "C"]
}
TRY ITLIVE · 50 ATOMS
Loading your default key…
The full key is used to call the gateway and stays in this tab — never sent to orbit or saved.
body*
graph*
The graph to analyze with the selected algorithm. All endpoints share the same `Graph` type, so that you can create the payload once and use it across all endpoints.
This algorithm works only on `directed` graphs, where edges have a direction from one vertex to another.
edges*
List of weighted edges between graph vertices, without duplicates. The order of vertices matters, A->B and B->A count as separate edges.
vertices*
List of up to 1000 vertices comprising the graph.

About this endpoint

What it does

Returns a topological ordering of the vertices in a directed graph. You send the graph definition in the request body, and the response contains an order array of vertex IDs.

Request Body

ParameterTypeMandatoryDescription
graphObjectYesThe graph to analyze. This endpoint works only on directed graphs.
graph.typeENUMYesGraph type. Allowed value: directed.
graph.verticesObject ArrayYesList of up to 1000 vertices comprising the graph. Each item has an id string matching ^[a-zA-Z0-9_:-]{1,40}$.
graph.vertices[].idStringYesUnique vertex ID, 1-40 characters. Pattern: ^[a-zA-Z0-9_:-]{1,40}$.
graph.edgesObject ArrayYesList of weighted edges between graph vertices, without duplicates. Edge direction matters, so A -> B and B -> A are separate edges.
graph.edges[].fStringYesStarting vertex of the directed edge. Pattern: ^[a-zA-Z0-9_:-]{1,40}$.
graph.edges[].tStringYesEnding vertex of the directed edge. Pattern: ^[a-zA-Z0-9_:-]{1,40}$.
graph.edges[].weightIntegerNoEdge weight. Default: 1. Range: 0 - 2^40. Unused in this algorithm.

Response

Returns a JSON object with an order string array field containing the topological ordering of the vertices.

ParameterTypeMandatoryDescription
orderString ArrayYesTopological ordering of the graph's vertices.
▣ ENDPOINT 04 / 06
POST
Finds the shortest path between vertices in a directed graph.
https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/shortest-path

QUICKSTART

GUIDE

Quickstart

Find the shortest path from one start vertex to another in a directed graph.

curl -X POST "https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/shortest-path?start=vertex_a&end=vertex_b" \
  -H "apy-token: $APY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "graph": {
      "type": "directed",
      "vertices": [
        { "id": "vertex_a" },
        { "id": "vertex_b" },
        { "id": "vertex_c" }
      ],
      "edges": [
        { "f": "vertex_a", "t": "vertex_c", "weight": 2 },
        { "f": "vertex_c", "t": "vertex_b", "weight": 3 }
      ]
    }
  }'

What you'll get back

Returns a JSON object in one of two shapes. For a single destination, it includes start, end, reachable as a boolean, distance as an integer or null, and path as an array of vertex IDs or null.

{
  "start": "vertex_a",
  "end": "vertex_b",
  "reachable": true,
  "distance": 5,
  "path": ["vertex_a", "vertex_c", "vertex_b"]
}

If no end is provided, it returns a JSON object with start and a paths object mapping each reachable vertex ID to an object containing distance and path.

TRY ITLIVE · 100 ATOMS
Loading your default key…
The full key is used to call the gateway and stays in this tab — never sent to orbit or saved.
body*
graph*
The graph to analyze with the selected algorithm. All endpoints share the same `Graph` type, so that you can create the payload once and use it across all endpoints.
This algorithm works only on `directed` graphs, where edges have a direction from one vertex to another.
edges*
List of weighted edges between graph vertices, without duplicates. For directed graphs, the order of vertices matters, A->B and B->A count as separate edges.
vertices*
List of up to 1000 vertices comprising the graph.
Start vertex ID
End vertex ID (if provided, the algorithm finds the shortest path only to that vertex)

About this endpoint

What it does

Finds the shortest path in a directed graph from the start vertex, using the graph provided in the request body. If the end query parameter is supplied, the response describes the shortest path to that specific vertex; otherwise, the response returns shortest-path results from the start vertex to all reachable vertices.

Query Parameter(s)

AttributeTypeMandatoryDescription
endStringNoEnd vertex ID. Pattern: ^[a-zA-Z0-9_:-]{1,40}$. If provided, the algorithm finds the shortest path only to that vertex.
startStringYesStart vertex ID. Pattern: ^[a-zA-Z0-9_:-]{1,40}$.

Request Body

ParameterTypeMandatoryDescription
graphObjectYesThe graph to analyze. Required fields: type, vertices, edges.
graph.typeENUMYesGraph type. Allowed value: directed.
graph.edgesObject ArrayYesList of weighted edges between graph vertices, without duplicates. For directed graphs, vertex order matters (A -> B and B -> A are separate edges).
graph.edges[].fStringYesStarting vertex ID of the directed edge. Pattern: ^[a-zA-Z0-9_:-]{1,40}$.
graph.edges[].tStringYesEnding vertex ID of the directed edge. Pattern: ^[a-zA-Z0-9_:-]{1,40}$.
graph.edges[].weightIntegerNoEdge weight, the cost of following the edge. Default: 1. Range: 0 - 2^40
graph.verticesObject ArrayYesList of up to 1000 vertices comprising the graph.
graph.vertices[].idStringYesUnique vertex ID. Pattern: ^[a-zA-Z0-9_:-]{1,40}$ (1-40 characters; letters, digits, underscores, colons, hyphens).

Response

Returns a JSON object. The response shape depends on whether end is supplied: either an object with start, end, reachable, distance, and path, or an object with start and paths.

ParameterTypeMandatoryDescription
startStringYesStart vertex ID.
endStringYesEnd vertex ID. Present in the single-target response shape.
reachableBooleanYesWhether the end vertex is reachable from start. Present in the single-target response shape.
distanceIntegerYesShortest-path distance. Nullable in the single-target response shape. In the multi-target response, each entry under paths includes a non-null integer distance.
pathString ArrayYesShortest path as an array of vertex IDs. Nullable in the single-target response shape. In the multi-target response, each entry under paths includes a path array.
pathsObjectYesMap of target vertex IDs to shortest-path results. Each value contains distance and path.
▣ ENDPOINT 05 / 06
POST
Finds the minimum spanning tree covering all vertices in a graph.
https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/minimum-spanning-tree

QUICKSTART

GUIDE

Quickstart

Find a minimum spanning tree for a simple weighted graph.

curl -X POST "https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/minimum-spanning-tree" \
  -H "apy-token: $APY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "graph": {
      "type": "undirected",
      "vertices": [
        { "id": "A" },
        { "id": "B" },
        { "id": "C" }
      ],
      "edges": [
        { "f": "A", "t": "B", "weight": 1 },
        { "f": "C", "t": "B", "weight": 2 },
        { "f": "A", "t": "C", "weight": 3 }
      ]
    }
  }'

What you'll get back

Returns a JSON object with an edges array and a totalWeight integer. edges contains the selected tree edges, each with f, t, and optionally weight; totalWeight is the sum of the chosen edge weights.

{
  "edges": [
    { "f": "A", "t": "B", "weight": 1 },
    { "f": "C", "t": "B", "weight": 2 }
  ],
  "totalWeight": 3
}
TRY ITLIVE · 50 ATOMS
Loading your default key…
The full key is used to call the gateway and stays in this tab — never sent to orbit or saved.

About this endpoint

What it does

Finds the minimum spanning tree for the graph you provide and returns the selected tree edges together with their combined weight.

Request Body

ParameterTypeMandatoryDescription
graphObjectYesThe graph to analyze. Include the graph type, vertices, and edges.
graph.typeENUMYesGraph type. Allowed values: directed, undirected.
graph.verticesObject ArrayYesList of up to 1000 vertices comprising the graph.
graph.vertices[].idStringYesUnique vertex ID, 1-40 characters. Allowed characters: letters, digits, underscores, colons, hyphens.
graph.edgesObject ArrayYesList of weighted edges between graph vertices, without duplicates. For directed graphs, edge direction matters.
graph.edges[].fStringYesOne of the two vertex IDs connected by the edge. For directed graphs, this is the starting vertex (from). Format: 1-40 characters; letters, digits, underscores, colons, hyphens.
graph.edges[].tStringYesOne of the two vertex IDs connected by the edge. For directed graphs, this is the ending vertex (to). Format: 1-40 characters; letters, digits, underscores, colons, hyphens.
graph.edges[].weightIntegerNoEdge weight / cost. Default: 1. Minimum: 0.

Response

Returns a JSON object with two required fields: edges is an array of edge objects, and totalWeight is an integer. The edges array contains the edges selected for the minimum spanning tree, and totalWeight is their combined weight.

ParameterTypeMandatoryDescription
edgesObject ArrayYesThe edges included in the minimum spanning tree. Each item contains f and t, and may include weight.
edges[].fStringYesFirst vertex ID for the returned edge.
edges[].tStringYesSecond vertex ID for the returned edge.
edges[].weightIntegerNoEdge weight. Default: 1. Range: 0 - 2^40
totalWeightIntegerYesTotal weight of the returned minimum spanning tree.

Notes

If you send a directed graph to this API:

  • The algorithm treats it as it were an undirected graph.
  • In case there are two directed edges A->B and B->A with different weights, the algorithm uses the edge with the lower weight. If the weights are equal, it takes the first of the edges defined in the input JSON.

The output JSON preserves the order of vertices in the edge definitions: if you define your undirected edge as B-A, and the edge is a part of the minimum spanning tree, it is listed in the output json as B-A, not as A-B.

The input graph must be connected.

▣ ENDPOINT 06 / 06
POST
Computes the distance matrix between all pairs of vertices in a graph.
https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/distance-matrix

QUICKSTART

GUIDE

Quickstart

Send a graph in the request body to calculate its distance matrix.

curl -X POST "https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/distance-matrix" \
  -H "apy-token: $APY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"graph":{"type":"directed","vertices":[{"id":"A"},{"id":"B"}],"edges":[{"f":"A","t":"B","weight":1}]}}'

What you'll get back

Returns a JSON object with either a distance-matrix-asymmetric object or a distance-matrix-symmetric object. Each of those is an object whose keys are vertex IDs and whose values are arrays of integers representing distances.

{
  "distance-matrix-asymmetric": {
    "A": [0, 1],
    "B": [-1, 0]
  }
}
TRY ITLIVE · 100 ATOMS
Loading your default key…
The full key is used to call the gateway and stays in this tab — never sent to orbit or saved.

About this endpoint

What it does

Computes the distance matrix for the graph provided in the request body. The response returns a JSON object containing one of:

  • distance-matrix-symmetric for undirected graphs (with the lower triangle of the distance matrix).
  • distance-matrix-asymmetric for directed graphs

Request Body

ParameterTypeMandatoryDescription
graphObjectYesThe graph to analyze. Contains the graph type, vertices, and edges.
graph.typeENUMYesGraph type. Allowed values: directed, undirected.
graph.verticesObject ArrayYesList of graph vertices, up to 384 vertices for directed graphs or 512 vertices for undirected graphs.
graph.vertices[].idStringYesUnique vertex ID. Format: 1-40 characters; letters, digits, underscores, colons, and hyphens (^[a-zA-Z0-9_:-]{1,40}$).
graph.edgesObject ArrayYesList of weighted edges between graph vertices, without duplicates. For directed graphs, edge order matters (A -> B and B -> A are separate edges).
graph.edges[].fStringYesOne endpoint of the edge. For directed graphs, this is the starting vertex (from). Format: 1-40 characters; letters, digits, underscores, colons, and hyphens (^[a-zA-Z0-9_:-]{1,40}$).
graph.edges[].tStringYesOne endpoint of the edge. For directed graphs, this is the ending vertex (to). Format: 1-40 characters; letters, digits, underscores, colons, and hyphens (^[a-zA-Z0-9_:-]{1,40}$).
graph.edges[].weightIntegerNoEdge weight, or cost of following the edge. Default: 1. Range: 0 - 2^40

Response

Returns a JSON object with one of two possible top-level fields: either distance-matrix-asymmetric (for directed graphs) or distance-matrix-symmetric (for undirected graphs), both of which are objects whose values are arrays of integers. The asymmetric form is used when distances from A to B may differ from B to A; the symmetric form is used when those distances are always the same.

ParameterTypeMandatoryDescription
distance-matrix-asymmetricObjectNoCalculated distance matrix where distances A -> B and B -> A may vary. The object's properties are additional keys that represent vertex ID-s, whose values are integer arrays: the distances to all points (see detailed notes below).
distance-matrix-symmetricObjectNoCalculated lower triangle of the distance matrix (below the diagonal) where distances A -> B and B -> A are always the same. The object's properties are additional keys that represent vertex ID-s, whose values are integer arrays: the distances to previous vertices in the matrix (see detailed notes below).

Notes

  • If the given two vertices are unreachable from each other, the returned distance for them is -1.
  • If there is an integer overflow during calculations, the API returns 422 Unprocessable Entity error.
  • Structure of distance-matrix-asymmetric object:
    • Each key is the ID of a single vertex from the input graph. The order of vertices in the object matters (see below).
    • The value for each key is an array of integers: distances too all points:
      • For the graph of N vertices, each array will have exactly N integers.
      • For I-th vertex, the I-th integer of an array is always 0: this is the distance from the given vertex to itself, on the diagonal.
      • The J-th integer corresponds to the J-th vertex from distance-matrix-asymmetric object.
  • Structure of distance-matrix-symmetric object:
    • Each key is the ID of a single vertex from the input graph. The order of vertices in the object matters (see below).
    • The value for each key is an array of integers: distances too all previous vertices:
      • For I-th vertex, the size of the integer array is always I-1 (the first vertex in distance-matrix-symmetric object always has the array of size 0).
      • The J-th integer corresponds to the J-th vertex in distance-matrix-symmetric object.
      • Effectively, the structure describes the lower triangle of the full distance matrix (below the diagonal). The API takes advantage of the fact that distances A->B and B->A are always the same in order to reduce the output JSON size approximately 2 times compared to returning the full distance matrix.
▣ COMMON ERRORS

Errors any endpoint can return

400bad_request

Required parameter missing or malformed body.

401unauthorized

API key missing, revoked, or not authorized for this service.

429rate_limited

Your plan's per-second rate exceeded. Retry with exponential backoff.

503upstream_busy

Backend temporarily unavailable. Try again in a few seconds.