Program your Robot with Viam's SDKs
Viam offers software development kits (SDKs) that wrap the viam-server
gRPC APIs and streamline connection, authentication, and encryption.
Use the SDK of your preferred language to write code to control your robots.
Viam currently offers SDKs for the following three languages:
Click on the links above to read more about installation and usage of each SDK.
Installation
pip install viam-sdk
go get go.viam.com/rdk/robot/client
npm install --save @viamrobotics/sdk
Note
Before you get started, ensure that you:
- Go to app.viam.com.
- Create a new robot.
- Go to the SETUP tab and follow the instructions there.
- Install either the Go, Python, or TypeScript SDK on your computer.
Tip
You can find more examples of Viam’s SDKs in the
Code Samples
The easiest way to get started is to navigate 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.
These boilerplate code samples import all of the necessary libraries and set up a client connection to your remote or local robot.
Caution
Do not share your robot secret or robot address publicly. Sharing this information compromises your system security by allowing unauthorized access to your computer.
Your boilerplate code sample should look similar to this:
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())
}
Note
The TypeScript SDK currently only support 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();
Add Control Logic
You can easily add control logic for each component of your robot by using the methods Viam has built into each component’s API specification.
Find documentation on how to use these methods here:
Run Your Code
After saving your boilerplate code sample and adding control logic with Viam’s API methods, run your program to control your Viam-connected robot.
For example:
python3 ~/myCode/myViamFile.py
go run ~/myCode/myViamFile.py
For an example, see this execution demo.