Next
Create a web app
Create and deploy a custom web interface for your machines without managing hosting infrastructure and authentication.
Once deployed, your application is accessible from a dedicated URL (appname_publicnamespace.viamapplications.com
), and Viam handles hosting and authentication.
Users log in and the application then renders your custom interface for interacting with the user’s machines. You can choose to build an application that can access and operate a single-machine or multiple machines.
You can build a custom web interface to access your machines using your preferred framework like React, Vue, Angular, or others.
Access machines from your application
When logging into a Viam application, Viam stores an access token in the user’s browser. For single-machine applications, Viam stores an access token and several machine-specific cookies.
Access the data from the browser’s cookies and use it to make API calls:
import * as VIAM from "@viamrobotics/sdk";
import Cookies from "js-cookie";
let apiKeyId = "";
let apiKeySecret = "";
let host = "";
let machineId = "";
async function main() {
const opts: VIAM.ViamClientOptions = {
serviceHost: "https://app.viam.com",
credentials: {
type: "api-key",
payload: apiKeySecret,
authEntity: apiKeyId,
},
};
const client = await VIAM.createViamClient(opts);
const machine = await client.appClient.getRobot(machineId);
}
document.addEventListener("DOMContentLoaded", async () => {
// Extract the machine identifier from the URL
let machineCookieKey = window.location.pathname.split("/")[2];
({
apiKey: { id: apiKeyId, key: apiKeySecret },
machineId: machineId,
hostname: host,
} = JSON.parse(Cookies.get(machineCookieKey)!));
main().catch((error) => {
console.error("encountered an error:", error);
});
});
import * as VIAM from "@viamrobotics/sdk";
import Cookies from "js-cookie";
let access_token = "";
// optionally specify a fragmentID to use with the listMachineSummaries API call
let fragmentID = "";
async function main() {
const opts: VIAM.ViamClientOptions = {
serviceHost: "https://app.viam.com",
credentials: {
type: "access-token",
payload: access_token,
},
};
const client = await VIAM.createViamClient(opts);
const locationSummaries = await client.appClient.listMachineSummaries("", [
fragmentID,
]);
}
document.addEventListener("DOMContentLoaded", async () => {
const userTokenRawCookie = Cookies.get("userToken")!;
const startIndex = userTokenRawCookie.indexOf("{");
const endIndex = userTokenRawCookie.indexOf("}");
const userTokenValue = userTokenRawCookie.slice(startIndex, endIndex + 1);
access_token = JSON.parse(userTokenValue).access_token;
main().catch((error) => {
console.error("encountered an error:", error);
});
});
Configure routing
When using your deployed application, static files will be accessible at https://your-app-name_your-public-namespace.viamapplications.com/machine/<machineHostname>/
.
When using your deployed application, static files will be accessible at https://your-app-name_your-public-namespace.viamapplications.com/
.
If your HTML file loads other files, use relative paths to ensure your files are accessible.
For developing your application on localhost:
Run your web server. Take note of its port number for the next step.
Run the following commands specifying the address where your app is running on localhost and a machine to test on.
The command will proxy your local app and open a browser window and navigate to http://localhost:8012/machine/<machineHostname>
for the machine provided with –machine-id.
viam login
viam module local-app-testing --app-url http://localhost:<PORT-YOUR-WEB-SERVER-IS-ON> --machine-id <MACHINE-ID>
viam login
viam module local-app-testing --app-url http://localhost:3000 --machine-id a1b2c3d4-e5f6-7890-abcd-ef1234567890
The command will proxy your local app and open a browser window and navigate to http://localhost:8012/
.
viam login
viam module local-app-testing --app-url http://localhost:<PORT>
viam login
viam module local-app-testing --app-url http://localhost:3000
To deploy your web interface with Viam, you must create a FILE>meta.json file and package it with yoru web application as a module.
Create a
Viam uses the
{
"module_id": "your-namespace:your-module",
"visibility": "public",
"url": "https://github.com/your-org/your-repo",
"description": "Your module description",
"applications": [
{
"name": "your-app-name",
"type": "single_machine|multi_machine",
"entrypoint": "dist/index.html",
"fragmentIds": [],
"logoPath": "static/logo.png",
"customizations": {
"machinePicker": {
"heading": "Heading to display on branded authentication page",
"subheading": "Subheading to display on branded authentication page"
}
}
}
]
}
{
"module_id": "acme:dashboard",
"visibility": "public",
"url": "https://github.com/acme/dashboard",
"description": "An example dashboard for a fictitious air quality company called acme.",
"applications": [
{
"name": "dashboard",
"type": "single_machine",
"entrypoint": "dist/index.html",
"fragmentIds": [],
"logoPath": "static/logo.png",
"customizations": {
"machinePicker": {
"heading": "Air monitoring dashboard",
"subheading": "Sign in and select your devices to view your air quality metrics in a dashboard."
}
}
}
]
}
This file specifies the contents of the module. It is required for your module.
The applications
field is an array of application objects with the following properties:
Property | Type | Description |
---|---|---|
name | string | The name of your application, which will be a part of the application’s URL (name_publicnamespace.viamapplications.com ). For more information on valid names see Valid application identifiers. |
type | string | The type of application: "single_machine" or "multi_machine" . Whether the application can access and operate one machine or multiple machines. |
entrypoint | string | The path to the HTML entry point for your application. The entrypoint field specifies the path to your application’s entry point. For example:
|
fragmentIds | []string | Specify the fragment or fragments that a machine must contain to be usable with a Viam application. |
logoPath | string | The URL or the relative path to the logo to display on the authentication screen for the application. |
customizations | object | Override the branding heading and subheading to display on the authentication screen:
{ "heading": "Air monitoring dashboard", "subheading": "Sign in and select your devices to view your air quality metrics in a dashboard" } . |
Register your module with Viam:
viam module create --name="app-name" --public-namespace="namespace"
viam module create --name="air-quality" --public-namespace="naomi"
To deploy your application with Viam you must package your static files and your
tar -czvf module.tar.gz <static-website-files> meta.json
viam module upload --upload=module.tar.gz --platform=any --version=0.0.1
For subsequent updates run these commands again with an updated version number.
After uploading your module with the application configuration, your application will be available at:
https://your-app-name_your-public-namespace.viamapplications.com
Users will be prompted to authenticate with their Viam credentials before accessing your application:
your-app-name_your-public-namespace.viamapplications.com
.your-app-name_your-public-namespace.viamapplications.com/machine/<machineHostname>
.
For multi-machine applications, the user is redirected to your-app-name_your-public-namespace.viamapplications.com/
.For a multi-machine TypeScript application see Monitor Air Quality with a Fleet of Sensors.
For a single-machine React application that shows camera feeds for a machine, see Viam Camera Viewer.
Viam does not currently support using custom domains (for example: app.mycustomdomain.com/machine/<machineHostname>
) to serve your Viam application.
You can, however, redirect from your domain to your Viam application (app.mycustomdomain.com
-> your-app-name_your-public-namespace.viamapplications.com
).
You can configure a redirect (HTTP 301) on your web server or hosting provider from app.mycustomdomain.com/*
to your-app-name_your-public-namespace.viamapplications.com/*
.
Was this page helpful?
Glad to hear it! If you have any other feedback please let us know:
We're sorry about that. To help us improve, please tell us what we can do better:
Thank you!