videorevitapipythonbim

What an API is — the bricks, and the hands that put them together

What an API is, explained with toy bricks: the box of pieces, the hands, the shape of the studs. And the three lines of Python that count the walls in a Revit model.

Paulo Giavoni

Paulo Giavoni

Engineer & BIM Specialist

19 August 20264 min read

Three letters you hear everywhere#

API. The Google API, the WhatsApp API, the Revit API. The full name is Application Programming Interface, and let's be honest: on its own, it says nothing. You need a picture, and the right picture has been in your home since you were six.

The bricks#

Two bricks: one comes down, lands on the other, click
Two bricks: one comes down, lands on the other, click
The library (the ready-made pieces), the language (the hands) and, in between, the API: the shape of the connections
The library (the ready-made pieces), the language (the hands) and, in between, the API: the shape of the connections
Take two LEGO pieces. One comes down, lands on the other, click. Keep that click in mind, because that's all there is to it.

On one side there's a box full of pieces someone else already built. In programming, that box is called a library. On the other side there's you, with your hands: the programming language, in our case Python. Two separate worlds, someone else's pieces and your hands. Where do they talk to each other?

They talk at the connection. Every brick has studs on top and tubes underneath, and those shapes are the same for every piece in the box. That's exactly what an API is: not the pieces, not the hands, but the shape of the connections.

Name, input, output#

The contract of the “round” connection: what you give it, 3.7; what it returns, 4
The contract of the “round” connection: what you give it, 3.7; what it returns, 4
The brick is sealed: you use the shape, never the contents
The brick is sealed: you use the shape, never the contents
Every connection tells you three things, always the same three:
  1. what it's called;
  2. what you have to give it;
  3. what it gives you back.

Take the “round” connection: name, round; input, a decimal number; output, the nearest whole number. This trio has a precise name: it's called a contract. If you can read a contract, you can use any library in the world.

And notice one thing: how the piece does the rounding, what math it runs inside, you don't know and you don't need to. The brick is sealed. You use the shape, never the contents.

Your day is full of contracts like this. The wall outlet: two holes, one shape for everyone, between whoever supplies the power and whoever uses it. Web APIs are the same idea, except the brick lives on a faraway computer.

And Revit?#

RevitAPI.dll: the box with Wall, Door, Level, Room, View, Floor
RevitAPI.dll: the box with Wall, Door, Level, Room, View, Floor
Now bring all of this inside Revit. For a programmer, Revit is a huge box of bricks: everything you see on screen, walls, doors, levels, views, is a piece Autodesk already built. The box has a label: RevitAPI.dll. And the bridge that brings your hands inside the box is a program like RevitPythonShell or pyRevit.

The first build: counting the walls#

Three lines, three connections: the model has 42 walls
Three lines, three connections: the model has 42 walls
The simplest question in the world: how many walls are in this model? Three connections, stacked one on top of the other:
  • FilteredElementCollector: opens the model's filing cabinet;
  • OfClass: you tell it which kind of piece you want, walls;
  • GetElementCount: you give it nothing, and it gives you a number.

Forty-two. The model has forty-two walls. Three lines, no magic: three contracts, read carefully.

And if you get it wrong? If you pass the collector a word instead of the document, Revit answers with an error message. That message is your friend: it's not the code breaking, it's the contract telling you what it expected.

The instruction booklet#

Where do these names come from? Every LEGO box has its booklet, and the Revit box has its own: the Revit API documentation, where every connection is written down with its name, input and output.

And the ladder is always the same: first you learn to read the model, then to filter and count, then to write. Always in that order, always with the same bricks. The box doesn't change: what changes is what you know how to build.

Watch the video#

Nine minutes, from the click of the bricks to your first code, no prerequisites. The video shows the pieces snapping together, the contract read line by line, the Revit box opening up and a real error on screen. The video is in Italian.

The full video is on YouTube: if you enjoyed it, subscribe to the channel and hit the bell for the next episodes.

Questions or comments?

Questions about the videos and the book are welcome: write to me, or comment under the video on YouTube.

On LinkedIn I post every video from the channel, with a few extra lines.