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:

  1. Add the following imports:

  1. Add the viam_client and other clients to the resource class:

  1. Initialize the clients and use them:

  1. Add the following imports:

  1. Add the viam_client and other clients to the resource class:

  1. 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:

  1. Create an API key with the appropriate permissions from your organization settings page.

  2. 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.

  1. Add the following imports and the create_robot_client_from_module method:

  1. Add the robot_client or other clients to the resource class:

  1. Initialize the client and use it:

  1. Add the following imports and the createRobotClientFromModule function:

  1. Add the viam_client and other clients to the resource class:

  1. Initialize the clients and use them:

To elevate access for the machine management API, follow the same steps described in Elevate access above.