Manage Machines with Viam's Robot API
The robot API is the application programming interface that manages each of your machines running viam-server
.
Use the robot API to connect to your machine from within a supported Viam SDK, and send commands remotely.
The robot API is supported for use with the Viam Python SDK, the Viam Go SDK, and the Viam C++ SDK.
Establish a connection
To interact with the robot API with Viam’s SDKs, instantiate a RobotClient
(gRPC client) and use that class for all interactions.
To find the API key, API key ID, and machine address, go to Viam app, select the machine you wish to connect to, and go to the Code sample tab. Toggle Include API key, and then copy and paste the API key ID and the API key into your environment variables or directly into the code:
You can use this code to connect to your machine and instantiate a RobotClient
that you can then use with the robot API.
As an example, this code uses the instantiated RobotClient
to return the resources currently configured.
Remember to always close the connection (using close()
) when done.
You can use this code to connect to your machine and instantiate a robot
client that you can then use with the Robot API.
As an example, this code uses the instantiated robot
client to return the configured resources.
Remember to always close the connection (using Close()
) when done.
Once you have instantiated the robot client, you can run any of the robot API methods against the robot
object to communicate with your machine.
Configure a timeout
Because the robot API needs to be able to reliably connect to a deployed machine even over a weak or intermittent network, it supports a configurable timeout for connection and command execution.
To configure a timeout when using the robot API:
Add a timeout
to DialOptions
:
The example above shows a timeout of 10 seconds configured.
Import the time
package, then add a timeout to your context using WithTimeout
:
The example above shows a timeout of 10 seconds configured.
API
The robot API support the following selected methods. For the full list of methods, see the Viam Python SDK documentation or the Viam Go SDK documentation.
Method Name | Description |
---|---|
GetOperations | Get the list of operations currently running on the machine. |
GetMachineStatus | Get status information about the machine. |
ResourceNames | Get a list of all known resource names connected to this machine. |
CancelOperation | Cancel the specified operation on the machine. |
BlockForOperation | Blocks on the specified operation on the machine. |
DiscoverComponents | Get a list of discovered component configurations. |
FrameSystemConfig | Get the configuration of the frame system of a given machine. |
TransformPose | Transform a given source Pose from the original reference frame to a new destination reference frame. |
TransformPCD | Transforms the pointcloud to the desired frame in the robot’s frame system. |
GetStatus | Get the status of the resources on the machine. |
StopAll | Cancel all current and outstanding operations for the machine and stop all actuators and movement. |
RestartModule | Reload a module as if its config changed. |
Log | Create a LogEntry object from the log to send to the RDK over gRPC. |
GetCloudMetadata | Get app-related information about the robot. |
GetVersion | Return version information about the machine. |
Options.with_api_key | Create a RobotClient.Options using an API key as credentials. |
AtAddress | Create a RobotClient that is connected to the machine at the provided address. |
WithChannel | Create a RobotClient that is connected to a machine over the given channel. |
Refresh | Manually refresh the underlying parts of this machine. |
Shutdown | Shutdown shuts down the machine. |
Close | Close the underlying connections and stop any periodic tasks across all constituent parts of the machine. |
GetOperations
Get the list of operations currently running on the machine.
Parameters:
- None.
Returns:
- (List[viam.proto.robot.Operation]): The list of operations currently running on a given machine.
Example:
For more information, see the Python SDK Docs.
GetMachineStatus
Get status information about the machine.
Parameters:
- None.
Returns:
- (viam.proto.robot.GetMachineStatusResponse): current status of the resources (List[ResourceStatus]) of the machine.
Example:
For more information, see the Python SDK Docs.
ResourceNames
Get a list of all known resource names connected to this machine.
Parameters:
- None.
Returns:
- ([]resource.Name): A list of all known resource names.
Example:
For more information, see the Go SDK Docs.
CancelOperation
Cancel the specified operation on the machine.
Parameters:
id
(str) (required): ID of operation to cancel.
Returns:
- None.
Example:
For more information, see the Python SDK Docs.
BlockForOperation
Blocks on the specified operation on the machine. This function will only return when the specific operation has finished or has been cancelled.
Parameters:
id
(str) (required): ID of operation to block on.
Returns:
- None.
Example:
For more information, see the Python SDK Docs.
DiscoverComponents
Get a list of discovered component configurations.
Parameters:
queries
(List[viam.proto.robot.DiscoveryQuery]) (required): The list of component models to lookup configurations for.
Returns:
- (List[viam.proto.robot.Discovery]): A list of discovered component configurations.
Example:
For more information, see the Python SDK Docs.
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.qs
([]resource.DiscoveryQuery): A list of tuples of API and model that you want to retrieve the component configurations corresponding to.
Returns:
- ([]resource.Discovery): The search query
qs
and the corresponding list of discovered component configurations as an interface calledResults
.Results
may be comprised of primitives, a list of primitives, maps with string keys (or at least can be decomposed into one), or lists of the forementioned type of maps. - (error): An error, if one occurred.
Example:
For more information, see the Go SDK Docs.
Parameters:
queries
(DiscoveryQuery[]): An array of tuples of API and model that you want to retrieve the component configurations corresponding to.
Returns:
- (Discovery[]): List of discovered component configurations.
For more information, see the Typescript SDK Docs.
FrameSystemConfig
Get the configuration of the frame system of a given machine.
Parameters:
additional_transforms
(List[viam.proto.common.Transform]) (optional): Any additional transforms.
Returns:
- (List[viam.proto.robot.FrameSystemConfig]): The configuration of a given machine’s frame system.
Example:
For more information, see the Python SDK Docs.
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
Returns:
- (*framesystem.Config): The configuration of the given machine’s frame system.
- (error): An error, if one occurred.
Example:
For more information, see the Go SDK Docs.
Parameters:
transforms
(Transform[]): An optional array of additional transforms.
Returns:
- (FrameSystemConfig[]): An array of individual parts that make up a machine’s frame system.
For more information, see the TypeScript SDK Docs.
TransformPose
Transform a given source Pose from the original reference frame to a new destination reference frame.
Parameters:
query
(viam.proto.common.PoseInFrame) (required): The pose that should be transformed.destination
(str) (required): The name of the reference frame to transform the given pose to.additional_transforms
(List[viam.proto.common.Transform]) (optional): Any additional transforms.
Returns:
- (viam.proto.common.PoseInFrame): The pose and the reference frame for the new destination.
Example:
For more information, see the Python SDK Docs.
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.pose
(*referenceframe.PoseInFrame): The pose that should be transformed.dst
(string): The name of the reference pose to transform the given pose to.additionalTransforms
([]*referenceframe.LinkInFrame): Any additional transforms.
Returns:
- (*referenceframe.PoseInFrame): Transformed pose in frame.
- (error): An error, if one occurred.
Example:
For more information, see the Go SDK Docs.
TransformPCD
Transforms the pointcloud to the desired frame in the robot’s frame system. Do not move the robot between the generation of the initial pointcloud and the receipt of the transformed pointcloud, as doing so will make the transformations inaccurate.
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.srcpc
(pointcloud.PointCloud): The sourcePointCloud
to transform.srcName
(string): The name of the source point cloud to transform.dstName
(string): The name of the destination point cloud.
Returns:
- (pointcloud.PointCloud): The transformed
PointCloud
. - (error): An error, if one occurred.
For more information, see the Go SDK Docs.
GetStatus
Get the status of the resources on the machine. You can provide a list of ResourceNames for which you want statuses. If no names are passed in, the status of every resource configured on the machine is returned.
Parameters:
components
(List[viam.proto.common.ResourceName]) (optional): Optional list of ResourceName for components you want statuses.
Returns:
- (List[viam.proto.robot.Status]): A list of statuses for each requested resource.
Example:
For more information, see the Python SDK Docs.
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.resourceNames
([]resource.Name): A list of resource names for components you want the status of. If no names are passed in, all resource statuses are returned.
Returns:
- ([]Status): The
Status
of each resource queried. If no resource was provided as a parameter, the status of all resources is returned. - (error): An error, if one occurred.
Example:
For more information, see the Go SDK Docs.
Parameters:
resourceNames
(commonApi.ResourceName[]): An optional array of ResourceNames for components you want the status of. If no names are passed in, all resource statuses are returned.
Returns:
- (robotApi.Status[]): An array containing the status of each resource.
For more information, see the TypeScript SDK Docs.
StopAll
Cancel all current and outstanding operations for the machine and stop all actuators and movement.
Parameters:
extra
(Mapping[str, Any]) (required): Extra options to pass to the underlying RPC call.
Returns:
- None.
Example:
For more information, see the Python SDK Docs.
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.extra
(map[string]interface{}): Extra options to pass to the underlying RPC call.
Returns:
- (error): An error, if one occurred.
Example:
For more information, see the Go SDK Docs.
RestartModule
Reload a module as if its config changed.
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.req
(RestartModuleRequest)
Returns:
- (error): An error, if one occurred.
For more information, see the Go SDK Docs.
Log
Create a LogEntry object from the log to send to the RDK over gRPC.
Parameters:
name
(str) (required): The logger’s name.level
(str) (required): The level of the log.time
(datetime.datetime) (required): The log creation time.message
(str) (required): The log message.stack
(str) (required): The stack information of the log.
Returns:
- None.
For more information, see the Python SDK Docs.
GetCloudMetadata
Get app-related information about the robot.
Parameters:
- None.
Returns:
- (viam.proto.robot.GetCloudMetadataResponse): App-related metadata.
Example:
For more information, see the Python SDK Docs.
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
Returns:
- (cloud.Metadata): App-related metadata containing the primary organization ID, location ID, and robot part ID for a machine running on the Viam app.
- (error): An error, if one occurred.
Example:
For more information, see the Go SDK Docs.
GetVersion
Return version information about the machine.
Parameters:
- None.
Returns:
- (viam.proto.robot.GetVersionResponse): Machine version related information.
Example:
For more information, see the Python SDK Docs.
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
Returns:
- (VersionResponse)
- (error): An error, if one occurred.
For more information, see the Go SDK Docs.
Options.with_api_key
Create a RobotClient.Options
using an API key as credentials.
Pass these options to AtAddress
.
Parameters:
api_key
(str) (required): your API key.api_key_id
(str) (required): your API key ID. Must be a valid UUID.
Returns:
- (typing_extensions.Self): the RobotClient.Options.
Raises:
- (ValueError): Raised if the api_key_id is not a valid UUID.
Example:
For more information, see the Python SDK Docs.
AtAddress
Create a RobotClient that is connected to the machine at the provided address.
Parameters:
address
(str) (required): Address of the machine (IP address, URL, etc.).options
(Options) (required): Options for connecting and refreshing.
Returns:
Example:
For more information, see the Python SDK Docs.
Parameters:
url
String (required)options
RobotClientOptions (required)
Returns:
Example:
For more information, see the Flutter SDK Docs.
WithChannel
Create a RobotClient that is connected to a machine over the given channel. Any machines created using this method will NOT automatically close the channel upon exit.
Parameters:
channel
(grpclib.client.Channel | viam.rpc.dial.ViamChannel) (required): The channel that is connected to a machine, obtained by viam.rpc.dial.options
(Options) (required): Options for refreshing. Any connection options will be ignored.
Returns:
Example:
For more information, see the Python SDK Docs.
Refresh
Manually refresh the underlying parts of this machine.
Shutdown
Shutdown shuts down the machine.
Supported by viam-micro-server
.
Parameters:
- None.
Returns:
- None.
Raises:
- (GRPCError): Raised with DeadlineExceeded status if shutdown request times out, or if the machine server shuts down before having a chance to send a response. Raised with status Unavailable if server is unavailable, or if machine server is in the process of shutting down when response is ready.
Example:
For more information, see the Python SDK Docs.
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
Returns:
- (error): An error, if one occurred.
Example:
For more information, see the Go SDK Docs.
Close
Close the underlying connections and stop any periodic tasks across all constituent parts of the machine.
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
Returns:
- (error): An error, if one occurred.
Example:
For more information, see the Go SDK Docs.
Have questions, or want to meet other people working on robots? Join our Community Discord.
If you notice any issues with the documentation, feel free to file an issue or edit this file.
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!