Python code example

Check out a Python example to see how to consume the Mural API.

In this section, we provide a Python example that demonstrates how to consume the Mural API. The example code:

  • Performs the OAuth 2.0 flow.
  • Retrieves a list of workspaces.
  • Creates five murals in a specified room.

To use this example, you must set your client_id, client_secret, scopes, and the roomId where you want the murals to live once created.

Additionally, ensure that you've added the redirect URL http://127.0.0.1:8000/ to the app you created in Mural.

The code will be:

from requests_oauthlib import OAuth2Session
from oauthlib.oauth2 import TokenExpiredError
from http.server import HTTPServer, BaseHTTPRequestHandler
import time
import http
from requests_toolbelt.utils import dump
import webbrowser;
import json;

class ServerHandler(BaseHTTPRequestHandler):
   def do_GET(self):
       self.send_response(200)
       self.end_headers()
       self.server.auth_response = self.requestline[4:-9]

# HTTP server for manage redirects
httpd = HTTPServer(('127.0.0.1', 8000), ServerHandler)

# Redirect URI
# Check this URL is part of the `redirect URLs` of your APP.
redirect_uri = 'http://127.0.0.1:8000/'

# Credentials of your APP
client_id = 'YOUR_CLIENT_ID'
client_secret = 'YOUR_CLIENT_SECRET'

# Room ID where murals will be created
roomId = 1111111111111111

# Mural OAuth2 endpoints
authorization_base_url = "https://app.mural.co/api/public/v1/authorization/oauth2/"
token_url = "https://app.mural.co/api/public/v1/authorization/oauth2/token"
refresh_url = "https://app.mural.co/api/public/v1/authorization/oauth2/refresh"

# Scopes required by the APP
scopes = [
       "murals:read",
       "murals:read",
       "murals:write",
       "rooms:read",
       "rooms:write",
       "templates:read",
       "templates:write",
       "workspaces:read",
       "users:read",
       "workspaces:write"
]

mural = OAuth2Session(client_id, scope=scopes, redirect_uri=redirect_uri)

# Redirect user to app.mural.co for authorization
authorization_url, state = mural.authorization_url(authorization_base_url)

# Open the web browser to the authorization URL.
# User will be asked to login and authorize the APP
webbrowser.open(authorization_url);

httpd.handle_request()

# Get the authorization code from the callback url
redirect_response = "https://127.0.0.1:8000" + httpd.auth_response

# Get the access token
token = mural.fetch_token(token_url, client_secret=client_secret, authorization_response=redirect_response)

# Start calling Mural API endpoints

# Get the list of workspaces
print('\nGet the list of workspaces:\n')

res = mural.get('https://app.mural.co/api/public/v1/workspaces')
workspaces = res.json().get("value");

print(json.dumps(workspaces, indent=4))

# Create 5 murals inside a room
print('\nCreate 5 murals inside a room:')

for i in range(1, 6):
       title = f"Mural {i} via API"
       # POST the title and roomId to create a new Mural
       res = mural.post("https://app.mural.co/api/public/v1/murals", json={"title": title, "roomId": roomId});
       newMural = res.json().get("value");
      
       print(f'\n{title}')
       print(json.dumps(newMural, indent=4))


# If you need to refresh the token
# mural.scope = None
# mural.refresh_token(refresh_url, client_id=client_id, client_secret=client_secret)