Skip to main content

How to Add the Teams JavaScript Library (TeamsJS) to a Web Page from the CDN

· 9 min read
Jagdish Kumawat
Founder @ Dewiride

A web page that runs inside Teams, in a tab or a dialog, talks to Teams through the Teams JavaScript library, TeamsJS. You can load it from Microsoft's CDN with one script tag, no build step needed. Here's the tag, the integrity hash that goes with it, and the call that connects the page to Teams.

What TeamsJS Is​

Microsoft describes the Teams JavaScript client library as a library that can help you create hosted experiences in Teams, Microsoft 365 app, and Outlook, where your app content is hosted in an iFrame. Your page runs in a frame inside Teams, and TeamsJS is how it asks Teams things, such as which theme is on, and tells Teams things, such as "close this dialog".

It comes as the npm package @microsoft/teams-js and as a file on Microsoft's CDN. The CDN file suits a plain HTML page: one <script> tag, nothing to install or bundle.

Syntax​

The script tag, then the call that connects the page to Teams:

Syntax
<script src="https://res.cdn.office.net/teams-js/<version>/js/MicrosoftTeams.min.js"
integrity="sha384-<hash>"
crossorigin="anonymous"></script>
<script>
await microsoftTeams.app.initialize();
</script>
  • <version> is the TeamsJS version, such as 2.60.0. Each version is its own file.
  • integrity="sha384-<hash>" is the file's Subresource Integrity hash. The browser runs the file only if it matches, so a changed file is blocked.
  • crossorigin="anonymous" fetches the file as a CORS request, without cookies. The browser checks the hash of a file from another site only on such a request: without the attribute, Chrome blocks the file.
  • microsoftTeams is the object the file adds to the page.
  • microsoftTeams.app.initialize() connects the page to Teams. It returns a promise; await waits for it, inside an async function, before any other TeamsJS call.

The Example​

The app Dialog Page Demo is a bot that opens a Teams dialog with a page it serves, host.html. The page starts with Connecting to Teams.... Once TeamsJS connects, it replaces that line with what Teams tells it: the host's name, the kind of client, and where the page is.

Prerequisites​

  • The page's domain in your app's validDomains. Teams loads a page in a dialog or tab only from a domain its manifest lists.

Step 1: Pick the Version​

Find the newest version of TeamsJS. With Node.js installed, ask npm:

Terminal
npm view @microsoft/teams-js version
Output
2.60.0

Without Node.js, the package's page on npm shows the same number. The CDN address uses it:

The file's address
https://res.cdn.office.net/teams-js/2.60.0/js/MicrosoftTeams.min.js

Microsoft's library reference shows the same tag for an earlier version, 2.59.0, with that version's hash. Copy its shape, not its numbers: each version needs its own hash.

Step 2: Make the Integrity Hash​

Download the file and hash it with SHA-384:

Terminal
curl -s -o MicrosoftTeams.min.js https://res.cdn.office.net/teams-js/2.60.0/js/MicrosoftTeams.min.js
echo "sha384-$(openssl dgst -sha384 -binary MicrosoftTeams.min.js | openssl base64 -A)"
Output
sha384-oM1pbPWSxyAWgEVI9GzHDR2xzdMMehbAdsl9cH5emcTxX9Qr/kE4IS/Ld5IdcLmy

Both give the same value. The same commands on version 2.59.0 give sha384-uF/e/QuPrfS8PQH8Tn/GHTrmwnGuEB7wAGqVCVj8Y39Fgwsz86rr5w4sZ1EN1ewZ, exactly the hash in Microsoft's reference, so the method matches theirs. How to Generate a Subresource Integrity (SRI) Hash for a Script in the Terminal explains each part of these commands, and what the browser does when the hash doesn't match.

Step 3: Add the Script Tag​

Put the tag in the page's <body>, after the content and before your own script, so the library has loaded by the time your script runs:

wwwroot/host.html (part)
<script src="https://res.cdn.office.net/teams-js/2.60.0/js/MicrosoftTeams.min.js"
integrity="sha384-oM1pbPWSxyAWgEVI9GzHDR2xzdMMehbAdsl9cH5emcTxX9Qr/kE4IS/Ld5IdcLmy"
crossorigin="anonymous"></script>

Step 4: Connect to Teams​

Below the tag, call microsoftTeams.app.initialize(), then read the page's context with microsoftTeams.app.getContext():

wwwroot/host.html (part)
<script>
async function start() {
await microsoftTeams.app.initialize();

const context = await microsoftTeams.app.getContext();
document.getElementById("status").textContent =
`Connected to ${context.app.host.name} (${context.app.host.clientType}). This page is in a ${context.page.frameContext}.`;
}

start();
</script>
  • async function start() wraps the calls, because await works inside an async function.
  • context.app.host.name is the app hosting the page, such as Teams.
  • context.app.host.clientType is the kind of client: Teams on the web, on desktop or on a phone.
  • context.page.frameContext is where the page runs, such as a tab's content or a dialog.
  • start(); runs it when the page loads.

The whole page:

A host.html file in a code editor window: a paragraph with the id status saying Connecting to Teams..., the TeamsJS 2.60.0 script tag from res.cdn.office.net with its sha384 integrity hash and crossorigin anonymous, and a script whose start function awaits microsoftTeams.app.initialize(), reads the context with getContext() and writes the host name, client type and frame context into the paragraph.

wwwroot/host.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Host check</title>
<style>
body { margin: 0; padding: 0 24px; font-family: "Segoe UI", system-ui, sans-serif; font-size: 14px; }
</style>
</head>
<body>
<p id="status">Connecting to Teams...</p>

<script src="https://res.cdn.office.net/teams-js/2.60.0/js/MicrosoftTeams.min.js"
integrity="sha384-oM1pbPWSxyAWgEVI9GzHDR2xzdMMehbAdsl9cH5emcTxX9Qr/kE4IS/Ld5IdcLmy"
crossorigin="anonymous"></script>
<script>
async function start() {
await microsoftTeams.app.initialize();

const context = await microsoftTeams.app.getContext();
document.getElementById("status").textContent =
`Connected to ${context.app.host.name} (${context.app.host.clientType}). This page is in a ${context.page.frameContext}.`;
}

start();
</script>
</body>
</html>

Step 5: Open the Page in Teams​

Open the page the way your app shows it. In the example, the bot's card button opens it in a dialog, in Teams on the web. Within two seconds, Connecting to Teams... changed to what TeamsJS reported:

A Teams dialog from the app Dialog Page Demo, titled Host check, showing the page's line: Connected to TeamsModern (web). This page is in a task.

  • TeamsModern is what Microsoft's reference calls Modern Teams: the new Teams.
  • web is the client that runs in a web browser.
  • task is a dialog. Dialogs were called task modules before, and the name stayed in TeamsJS.

The page is connected, and any other TeamsJS call can follow initialize().

What Happens Without app.initialize()​

A page that loads TeamsJS but never calls microsoftTeams.app.initialize() still shows in the dialog, but it can't talk to Teams. About 30 seconds later, Teams on the web adds a warning on top of it: If you're having issues with this app, try the desktop version. Microsoft's requirements for tabs ask pages to call microsoftTeams.app.initialize() to avoid seeing a warning in the new domain. Fixed: "If You're Having Issues with This App, Try the Desktop Version" in a Teams Dialog shows it.

Common Mistakes​

MistakeWhat happensDo this instead
Copying the tag from the docs and changing only the versionThe hash belongs to the other version, so the browser blocks the fileMake the hash for the version you use
Leaving out crossorigin="anonymous"Chrome blocks the file, because it can't check the hash of a file from another site without CORSKeep all three attributes
Never calling initialize()Teams on the web warns try the desktop version after about 30 secondsCall it as soon as the library loads

Troubleshooting​

SymptomLikely causeFix
The page stays at its first text, such as Connecting to Teams...The script stopped: the library didn't load, or a call failedOpen the browser console and read the first error
The console says Failed to find a valid digest in the 'integrity' attributeThe hash doesn't match the file at that addressMake the hash again for that exact address
The console says Subresource Integrity: The resource '…' has an integrity attribute, but the resource requires the request to be CORS enabled to check the integrity, and it is not. The resource has been blocked because the integrity cannot be enforced.The tag has integrity but no crossoriginAdd crossorigin="anonymous"
If you're having issues with this app, try the desktop version over the pageThe page didn't call app.initialize()Call it when the page loads

FAQ​

Should I use the npm package instead?​

Both are official. Microsoft's reference says the library is distributed as an npm package … It can also be referenced from its CDN endpoint. Use npm when your page is built with a bundler: since version 2.31.0 the library is tree-shakeable, so a bundle keeps only what you use. For a plain HTML page, the CDN tag is simpler.

Why does the address pin a version?​

The integrity hash is for one exact file. Each version is a different file with a different hash: 2.59.0 and 2.60.0 hash differently, as in Step 2. Moving to a new version means a new address and a new hash.

Does this work in a tab too?​

Microsoft's overview names tabs and dialogs as the two kinds of Teams pages the library is for, and a tab's page loads it the same way. The example here runs in a dialog.

Conclusion​

Load TeamsJS from https://res.cdn.office.net/teams-js/<version>/js/MicrosoftTeams.min.js with an integrity hash you make for that version and crossorigin="anonymous", then await microsoftTeams.app.initialize() before any other call. After that, microsoftTeams.app.getContext() tells the page where it runs.

Additional Resources​