Previous
Alert on detections
Modules extend what your machine can do. They come in two varieties: driver modules that add support for new hardware, and logic modules that tie components together with decision-making code. You can develop modules in your own IDE or write them directly in the browser using inline modules.
If you have already configured components on your machine, each one works individually: you can test it from the Viam app, capture its data, and call its API from a script. The next step is making them work together. A camera detects an object, and a motor responds. A temperature sensor crosses a threshold, and a notification fires. A movement sensor reports position, and an arm adjusts.
A driver module teaches Viam how to talk to a specific piece of hardware. Every module implements one of Viam’s resource APIs. For a driver module, you pick the component API that matches your hardware:
| API | Use when your hardware… | Key methods |
|---|---|---|
sensor | Produces readings (temperature, distance, humidity) | GetReadings |
camera | Produces images or point clouds | GetImage, GetPointCloud |
motor | Drives rotational or linear motion | SetPower, GoFor, Stop |
arm | Has joints and moves to poses | MoveToPosition, MoveToJointPositions |
base | Is a mobile platform (wheeled, tracked, legged) | MoveStraight, Spin, SetVelocity |
Viam defines over 15 component APIs and 10 service APIs. For the full list, see Resource APIs.
Each implementation of a resource API is called a model. For example,
the camera API has models for USB cameras, CSI cameras, RTSP streams, and
others. When no existing model supports your hardware,
you write a driver module to add one. Once it exists, the hardware behaves
like any built-in component. Data capture, test panels, and the SDKs work
automatically.
A logic module controls your machine’s behavior. It declares dependencies on the resources it needs and implements your application’s decision-making. Many logic modules run continuously on your machine, reading from sensors, evaluating conditions, and commanding actuators.
Use a logic module when you need your machine to:
Logic modules typically implement the generic service API. The generic API
has a single method, DoCommand, which accepts and returns arbitrary key-value
maps. Use it to check status, adjust parameters, or send commands to your
running module from external scripts or the Viam app:
// Request
{"command": "get_alerts", "severity": "critical"}
// Response
{"alerts": [{"sensor": "temp-1", "value": 42.5, "threshold": 40.0}]}
Use generic when your module’s interface does not map to an existing service
API (like vision or mlmodel).
There are two ways to develop and deploy modules:
Inline modules let you write code directly in the Viam app’s browser-based editor. Viam manages source code, builds, versioning, and deployment. When you click Save & Deploy, the module builds in the cloud and deploys to your machine automatically. Inline modules are the fastest way to get started, especially for prototyping and simple control logic.
Externally managed modules are modules you develop in your own IDE, manage in your own git repository, and deploy through the Viam CLI or GitHub Actions. Use externally managed modules when you need your own source control, public distribution, or custom build pipelines.
| Inline | Externally managed | |
|---|---|---|
| Where you write code | Browser editor in the Viam app | Your own IDE, locally or in a repo |
| Source control | Managed by Viam | Your own git repository |
| Build system | Automatic cloud builds on save | Cloud build (GitHub Actions) or local builds |
| Versioning | Automatic (0.0.1, 0.0.2, …) | You choose semantic versions |
| Visibility | Private to your organization | Private or public |
Both types run identically at runtime, as child processes communicating with
viam-server over gRPC.
Every module goes through a defined lifecycle:
viam-server launches the module as a separate process. The
module registers its models and opens a gRPC connection back to the server.viam-server calls the
model’s config validation method to check attributes and declare
dependencies.viam-server calls the model’s
constructor with the resolved dependencies.viam-server
calls the validation method again, then the reconfiguration method.viam-server calls the resource’s close method. Clean up
resources here.For the full lifecycle reference including crash recovery, first-run scripts, and timeouts, see Module developer reference.
Attributes are the user-provided configuration for your resource. When someone adds your module to a machine, they set attributes in the Viam app or in the machine’s JSON config. Examples include a device address for a driver module, or a polling interval and threshold for a logic module.
Your module defines which attributes it expects and validates them in its
config validation method. If validation fails, viam-server reports the error
and does not create the resource. Attributes are passed to your constructor
when the resource is created and again to your reconfiguration method when
the configuration changes.
For code examples, see the attribute definitions in Write a driver module and Write a logic module.
Dependencies let your resource use other resources on the same machine. You
declare dependencies in your config validation method by returning the names of
resources your module needs. viam-server resolves these, ensures the
depended-on resources are ready, and passes them to your constructor.
viam-server
retries every 5 seconds and reconfigures your resource when the dependency
becomes available.The pattern has three steps:
For detailed code examples, see Module dependencies.
The Viam module registry stores versioned module packages and serves them to
machines on demand. When you configure a module on a machine, viam-server
downloads the correct version for the machine’s platform (OS and architecture).
Modules can be:
The registry uses semantic versioning. Machines can track the latest version (automatic updates) or pin to a specific version.
Logic modules often need to run continuously: polling sensors, checking thresholds, updating state. You can spawn background tasks (goroutines in Go, async tasks in Python) from your constructor or reconfiguration method.
The key requirement: your background task must stop cleanly when the module
shuts down or reconfigures. Use a cancellation signal (a context cancellation
in Go, an asyncio.Event in Python) to coordinate this.
Was this page helpful?
Glad to hear it! If you have any other feedback please let us know:
We're sorry about that. To help us improve, please tell us what we can do better:
Thank you!