> For the complete documentation index, see [llms.txt](https://docs.orkestra.online/orkestra-online/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.orkestra.online/orkestra-online/okpy/get-started/first-tool-in-code.md).

# Write your first tool in code

Create a tool in the OkPy editor, write a few lines of C# or Python against your design app's API, run it, and read the console.

This page walks you through a first tool in the OkPy editor: it counts the walls in your Revit model and prints the result. The same steps work in Civil 3D, AutoCAD, Navisworks and Rhino with their own API.

You don't need an account to write and run tools on your computer. You need one to publish them.

{% stepper %}
{% step %}

### Create the tool

1. Open your model and click **OkPy** on the Orkestra ribbon. See [Open OkPy](https://docs.orkestra.online/orkestra-online/okpy/get-started/open-okpy.md).
2. On the home screen, click **New Tool** (or **File** > **New**).
3. In the **New tool** dialog, pick a language:
   * **C#** (Recommended): calls the design app's API directly, runs faster on big jobs, catches typos before it runs, and is the only language for [panel tools and event tools](https://docs.orkestra.online/orkestra-online/okpy/kinds-of-tools.md).
   * **Python**: Python 3, a good pick if you already know Python.
4. In **Start from**, keep **Blank tool**, or pick a template such as "OkPy C# Tool Template For Revit".
5. Click **Create**.

You can't switch a tool between C# and Python later: choose before you create it.

<figure><img src="https://705252832-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M6wDJF-clP9xeoy68-u%2Fuploads%2FzF2CyHVFRjTOJr3Duq95%2Fscreenshots-okpy-new-tool-dialog.png?alt=media" alt="The New tool dialog: Pick a language and a starting point. The C# card, marked Recommended, lists Native .NET, Compiled, Typos caught before it runs, and Event tools and panel tools: only C# tools; the Python card says Python 3. A good pick if you already know Python. Start from: Blank tool. Buttons Cancel and Create."><figcaption><p>The New tool dialog.</p></figcaption></figure>
{% endstep %}

{% step %}

### Write the code

The code goes in the centre of the editor. Your code can use the open model straight away: in C# it is `doc`, in Python `__currentdoc__`. See [What your code can use](https://docs.orkestra.online/orkestra-online/okpy/write-code/script-globals.md).

{% tabs %}
{% tab title="C#" %}

```csharp
var walls = new FilteredElementCollector(doc)
    .OfClass(typeof(Wall))
    .ToElements();

print($"This model has {walls.Count} walls.");
```

The Revit namespaces (`Autodesk.Revit.DB` and others) are already imported for you.
{% endtab %}

{% tab title="Python" %}

```python
import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import FilteredElementCollector, Wall

doc = __currentdoc__
walls = FilteredElementCollector(doc).OfClass(Wall).ToElements()

print("This model has {} walls.".format(walls.Count))
```

{% endtab %}
{% endtabs %}

As you type, the editor suggests names (**Ctrl+Space** shows them). A mistake shows in the status bar under the code ("1 problem"): click it, or press **F8**, to jump to it.
{% endstep %}

{% step %}

### Run it

Click **Run** (the play button) or press **F5**. The console under the code shows:

```
This model has 142 walls.
```

If the code fails, the error shows in red in the console and on its line in the code. Right-click it and choose **Fix with OkPy Agent** if you want help.

{% hint style="warning" %}
**Run** works on your model for real. This tool only reads, but a tool that changes the model keeps its changes (use Undo in your design app). Test Mode, which undoes everything afterwards, is offered by OkPy Agent once it has built or changed a tool. See [Run, Test and Preview](https://docs.orkestra.online/orkestra-online/okpy/the-editor/run-test-and-preview.md).
{% endhint %}
{% endstep %}

{% step %}

### Name and save it

1. Click the name at the top of the editor, type "Wall Count" and press **Enter**. Click the line under it to add a description.
2. Click **Save** (or **Ctrl+S**) and choose where to keep the `.okpy` file.

To share the tool with your team, click **Publish** instead. See [Publish to Orkestra](https://docs.orkestra.online/orkestra-online/okpy/save-and-share/publish-to-orkestra.md).
{% endstep %}
{% endstepper %}

## Next steps

* Give your tool a window with buttons and lists: [Tools with their own window](https://docs.orkestra.online/orkestra-online/okpy/kinds-of-tools/tools-with-a-window.md).
* Change the model safely, with transactions: [Change the model safely](https://docs.orkestra.online/orkestra-online/okpy/write-code/change-the-model.md).
* Ready-made helpers for Excel, PDF, Autodesk Construction Cloud and more: [OkPy Toolkit](https://docs.orkestra.online/orkestra-online/okpy/write-code/toolkit.md).
* Let the agent write or fix code with you: [The Agent panel in the editor](https://docs.orkestra.online/orkestra-online/okpy/okpy-agent/agent-panel.md).

## Related pages

* [Tour of the editor](https://docs.orkestra.online/orkestra-online/okpy/the-editor/tour-of-the-editor.md)
* [Write and edit code](https://docs.orkestra.online/orkestra-online/okpy/the-editor/write-and-edit-code.md)
* [C# and Python](https://docs.orkestra.online/orkestra-online/okpy/write-code/csharp-and-python.md)
