Access platform APIs from within a module | Viam Documentation
Access platform APIs from within a module
To use the platform or machine APIs, you must authenticate using API keys.
Use platform APIs from a module
The following steps show you how to use the following APIs from a module:
- Add the following imports:
- Add the
viam_clientand other clients to the resource class:
- Initialize the clients and use them:
- Add the following imports:
- Add the
viam_clientand other clients to the resource class:
- Initialize the clients and use them:
Elevate access
The module environment variables VIAM_API_KEY and VIAM_API_KEY_ID provide machine owner access for the machine the module is running on.
If you need a higher level of access, you can pass API keys as part of the module configuration:
Create an API key with the appropriate permissions from your organization settings page.
Add the API key and API key ID values to the module configuration:
{
"modules": [\
{\
"type": "registry",\
"name": "example-module",\
"module_id": "naomi:example-module",\
"version": "latest",\
"env": {\
"VIAM_API_KEY": "abcdefg987654321abcdefghi",\
"VIAM_API_KEY_ID": "1234abcd-123a-987b-1234567890abc"\
}\
}\
]
}
This changes the environment variables VIAM_API_KEY and VIAM_API_KEY_ID from the default to the provided ones.
Use the machine management API from a module
To use the machine management (robot_client) API, you must get the machine’s FQDN and API keys from the module environment variables.
- Add the following imports and the
create_robot_client_from_modulemethod:
- Add the
robot_clientor other clients to the resource class:
- Initialize the client and use it:
- Add the following imports and the
createRobotClientFromModulefunction:
- Add the
viam_clientand other clients to the resource class:
- Initialize the clients and use them:
To elevate access for the machine management API, follow the same steps described in Elevate access above.