Next, to install your preferred Viam SDK on your Linux or macOS development machine or single-board computer, run one of the following commands in your terminal:
If you are using the Python SDK, set up a virtual environment to package the SDK inside before running your code, avoiding conflicts with other projects or your system.
Create a program in the language of your choice that connects to your robot and uses methods built into the SDK’s client API libraries to interact with and control the resources on the robot.
Start by navigating to your robot’s page on the Viam app.
Select the Code Sample tab, select your preferred SDK, and copy the code generated for you.
This code snippet imports all the necessary libraries to set up a connection with your robot and interface with its configured components and services.
Your boilerplate code sample should look similar to this:
Click this to see example boilerplate code from the Code Sample tab
import asyncio
from viam.robot.client import RobotClient
from viam.rpc.dial import Credentials, DialOptions
async def connect():
creds = Credentials(
type='robot-location-secret',
payload='SECRET FROM THE VIAM APP')
opts = RobotClient.Options(
refresh_interval=0,
dial_options=DialOptions(credentials=creds)
)
return await RobotClient.at_address('ADDRESS FROM THE VIAM APP', opts)
async def main():
robot = await connect()
print('Resources:')
print(robot.resource_names)
await robot.close()
if __name__ == '__main__':
asyncio.run(main())
package main
import (
"context"
"github.com/edaniels/golog"
"go.viam.com/rdk/robot/client"
"go.viam.com/rdk/utils"
"go.viam.com/utils/rpc"
)
func main() {
logger := golog.NewDevelopmentLogger("client")
robot, err := client.New(
context.Background(),
"ADDRESS FROM THE VIAM APP",
logger,
client.WithDialOptions(rpc.WithCredentials(rpc.Credentials{
Type: utils.CredentialsTypeRobotLocationSecret,
Payload: "SECRET FROM THE VIAM APP",
})),
)
if err != nil {
logger.Fatal(err)
}
defer robot.Close(context.Background())
logger.Info("Resources:")
logger.Info(robot.ResourceNames())
}
Info
The TypeScript SDK currently only supports building web browser apps.
import { Client, createRobotClient, RobotClient } from "@viamrobotics/sdk";
async function connect() {
// You can remove this block entirely if your robot is not authenticated.
// Otherwise, replace with an actual secret.
const secret = "<SECRET>";
const credential = {
payload: secret,
type: "robot-location-secret",
};
// Replace with the host of your actual robot running Viam.
const host = "<HOST>";
// Replace with the signaling address. If you are running your robot on Viam,
// it is most likely https://app.viam.com:443.
const signalingAddress = "https://app.viam.com:443";
const iceServers = [{ urls: "stun:global.stun.twilio.com:3478" }];
return createRobotClient({
host,
credential,
authEntity: host,
signalingAddress,
iceServers,
});
}
async function main() {
// Connect to client
let client: Client;
try {
client = await connect();
console.log("connected!");
let resources = await client.resourceNames();
console.log("Resources:");
console.log(resources);
} catch (error) {
console.log(error);
return;
}
}
main();
Stability Notice
The C++ SDK is currently in alpha.
# include <string>
# include <vector>
# include <boost/optional.hpp>
# include <viam/api/common/v1/common.pb.h>
# include <viam/api/robot/v1/robot.grpc.pb.h>
# include <viam/sdk/robot/client.hpp>
# include <viam/sdk/components/camera/client.hpp>
using namespace viam::sdk;
int main() {
std::string host("ADDRESS FROM THE VIAM APP");
DialOptions dial_opts;
Credentials credentials("SECRET FROM THE VIAM APP");
dial_opts.set_credentials(credentials);
boost::optional<DialOptions> opts(dial_opts);
Options options(0, opts);
auto robot = RobotClient::at_address(host, options);
std::cout << "Resources:\n";
for (const ResourceName& resource: *robot->resource_names()) {
std::cout << resource.namespace_() << ":" << resource.type() << ":"
<< resource.subtype() << ":" << resource.name() << "\n";
}
return 0;
}
Stability Notice
The Flutter SDK is currently in beta.
// This must be run from inside an existing app,
// e.g. the default Flutter app created by `flutter create APP_NAME`
// Step 1: Import the viam_sdk
import 'package:viam_sdk/viam_sdk.dart';
// Step 2: Call this function from within your widget
Future<void> connectToViam() async {
const host = 'billowing-brook-main.7kp7y4p393.viam.cloud';
// Replace '<SECRET>' (including brackets) with your robot's secret
const secret = '<SECRET>';
final robot = await RobotClient.atAddress(
host,
RobotClientOptions.withLocationSecret(secret),
);
// Print the available resources
print(robot.resourceNames);
}
Save this file on your development machine with the file type of your preferred SDK.
The sample code contains the required imports as well as the connect logic which establishes a connection for your client application to communicate with the robot’s viam-server instance.
This section of the boilerplate code contains your robot’s address and location secret.
You can think of these as keys or access tokens to your robot that are important to keep private.
This connection must be established for your program to be executed properly on your robot.
Caution
Do not share your location secret, part secret, or robot address publicly.
Sharing this information could compromise your system security by allowing unauthorized access to your robot, or to the computer running your robot.
can establish a connection to your robot through the cloud, on a local or wide area network (LAN or WAN), or locally
The program will connect to your robot and print a list of the available resources.
Edit the sample code
Once you have successfully run the sample code, you can edit the boilerplate code by adding control logic to make a client application that connects to your robot and controls it in the way you want.
You can find the right libraries to import for SDK methods, typing, interfaces, and utilities at the start of each resource’s API documentation, as well as in the individual SDK documentation sites and on GitHub.