Postman tutorial for Viam's gRPC APIs
Postman tutorial for Viam's gRPC APIs
1. Overview
You can use the Viam web app to control your machines, or the Viam SDKs to develop frontend and backend applications. But if you want to explore what's happening behind the scenes, let's use Viam's gRPC API to control a rover.
Prerequisites
- Sign up for a free Viam account, and then sign in to the Viam app.
- Set up a rover, such as Yahboom or SCUTTLE or Viam rover
- Sign up for a free Postman account, and then sign in. If you're using the web version of Postman, also download and install the Postman desktop agent.
What You'll Learn
- Authorize the Viam gRPC API
- Invoke API methods available for your machine's resources
- Use variables in Postman to store data
What You'll Need
What You'll Build
- A working Postman collection to control and interact with your Viam machine
Watch the Video
Follow along with the step-by-step video.
Spin a rover with the Viam gRPC API
2. Fork the sample collection
In Postman, fork the following collection to your own workspace.
- Select the collection in the sidebar.
- Select the overflow menu ( ...), and Fork the collection.
- Enter a label for your fork, and select a workspace to save it to.
- Select Fork Collection.
Postman creates your fork in the selected workspace. You can now update this version of the collection as you wish. If you optionally chose to watch the collection, you can pull changes to your own collection when the main collection is updated.
3. Authorize API requests
Find your Viam credentials
- To find the API key and API key ID, go to the Viam app, select the machine you wish to connect to, and under the Connect tab, select a code sample language. Toggle Include API key, and then copy and paste the API key ID and API key to update your Postman variables
- To find the machine address, go to the Viam app, select the machine you wish to connect to, and under the Status dropdown, copy the Remote address to update your Postman variable.
- Input your credentials for
api_key,api_key_id, andmachine_urlas Postman variables and Save your changes.
Authorization
To authorize subsequent calls to the Viam server running on your smart machine, generate access tokens in Postman.
- Invoke the
AuthService / Authenticatemethod to generate an access token. Under the Messages tab, notice you are using yourapi_keyandapi_key_idto authorize the call to theapp_url. Under the Scripts tab, notice code that saved the access token as a variableapp_access_tokenfor you to re-use in subsequent requests. - Invoke the
ExternalAuthService / AuthenticateTomethod to generate an access token. Under the Scripts tab, notice code that saved the access token as a variablemachine_access_token. - For subsequent calls to Viam gRPC services, you can include the
machine_access_tokenvariable under the Authorization tab expressed with double curly braces like{{machine_access_token}}using theBearer tokenauthorization type in order to make calls to yourmachine_url.
4. Spin a Viam rover
Get resource names
- Invoke the
RobotService / ResourceNamemethod to get a list of resources associated with your machine. Notice you are using yourmachine_access_tokento authorize the call to yourmachine_url. - In the server response, find the
basecomponent, and make a note of the component'sname. We will need it for the next step.
Spin a Viam rover
- Open the
BaseService / Spinrequest. Once again, you are using yourmachine_access_tokento authorize the call to yourmachine_url. This time, additional information is required. Under the Messages tab of your request, update thenameproperty to the name of your ownbasecomponent retrieved in the previous step. - Invoke the method to spin your rover.
From here, you can continue updating the payload, exploring other hardware Components, software Services, and experiment with other cloud capabilities, such as machine learning.
5. Next Steps
This tutorial was a hands-on introduction to the Viam gRPC API. Browse other methods available using server reflection, or see some of them documented in the Viam gRPC API collection as a reference. Read more about how Viam uses gRPC and WebRTC for system architecture.
In most cases, you won't be developing applications or controlling them using the raw gRPC APIs. Instead, check out these resources:
- The Viam SDK libraries for development
- The Viam web app for ad hoc controls
Once you learn the fundamentals of working with your machine, you can start adding more intelligence and capabilities, such as the following.
- Viam Data Management: Collect data from different parts of a robot, IoT device, or any other machine
- Viam Fleet management: Organize, manage, and control any number of machines alone or in collaboration with others
- Viam Registry: Add and share a modular resource on your robot