You are currently viewing the new Anvil Editor Docs.
Switch to the Classic Editor Docs
You are currently viewing the Classic Editor Docs.
Switch to the new Anvil Editor Docs

Quickstart: Microsoft APIs

Use Active Directory in your Anvil app

Anvil provides a one-line login function to log users in to their Microsoft account. You can also restrict access to only users in your own Active Directory. You can easily use Microsoft Azure’s many REST APIs from Anvil client or server code.

Follow this quickstart to enable the Microsoft Service and set up a login that uses Microsoft’s Single Sign On system.

Create an app

Log in to Anvil and click ‘Blank App’. Choose the Material Design theme.

Location of the Create App button

Location of the Create App button

Enable the Microsoft API Service

To add the Microsoft API service to your app, click the blue plus button Add button sidebar menu in the Sidebar Menu.

You’ll see a list of available services and integrations. Click on Microsoft API Microsoft API Icon.

Services list with Microsoft highlighted

In the App Browser, click the + next to Services.

App Browser showing the plus next to Services

You’ll see a list of available services and integrations. Click on Microsoft API.

Services list with Microsoft highlighted

Call the Active Directory login function

Click on Form1 in the App Browser.

Form1 in the App Browser

Form1 in the App Browser

Go to the Code view to see the code for Form1.

Form1 in the App Browser

Form1 in the App Browser

In the __init__ method, add these lines:

    anvil.microsoft.auth.login()

    print(f"You are logged in as: {anvil.microsoft.auth.get_user_email()}")

Run your app and log in

Now click the ‘Run’ button at the top of the screen.

Run app button at the top of the Anvil Editor

Run app button at the top of the Anvil Editor

Your app will run and display a dialog informing you that you are about to log in with Microsoft.

A running app showing a dialog with 'cancel' and 'log in' buttons, and a message informing you you are about to log in with Microsoft.

A running app showing a dialog with 'cancel' and 'log in' buttons, and a message informing you you are about to log in with Microsoft.

Click ‘Log In’. The Microsoft Single Sign On page will open in a new browser window. Log in with a Microsoft account (if you don’t already have a Microsoft account, you can create one using this window.)

You are now logged into your app. The App Console has printed the email address you logged in with:

The App Console with a message giving the email address you are logged in with via Microsoft Azure.

You are now logged into your app. The Output Panel has printed the email address you logged in with:

The Output Panel with a message giving the email address you are logged in with via Microsoft Azure.

Copy the example app

Click on the button below to clone a finished version of this app into your account.

Next Up

Want more depth on this subject?

You can restrict access to only users in your own Azure Active Directory. Read Using your own Azure Active Directory for more details.

It’s easy to access Microsoft Azure APIs via your Active Directory. Anvil fetches an API token for you, so you just have to make the relevant HTTP requests. Read Accessing Microsoft APIs for more details.

Want another quickstart?

Every quickstart is on the Quickstarts page.


Do you still have questions?

Our Community Forum is full of helpful information and Anvil experts.