Viewing Vanity URLs

Problem

You want to view the Vanity URL associated with your Content.

Solution

Note

Requires posit-sdk>=0.6.0

Obtain the content object for your Content. In this example, we get the Content using it’s GUID.

from posit import connect

client = connect.Client()

CONTENT_GUID = "154bd2af-e8fa-4aa4-aab8-dcef701f4af9"

content = client.content.get(CONTENT_GUID)

Then read the content.vanity attribute to view the Vanity URL path.

>>> content.vanity
"/my-dashboard/"

If the Content does not have a Vanity URL, the value is None.

>>> print(vanity)
None

Use the Content GUID to look up its Vanity URL.

library(connectapi)

CONTENT_GUID <- "154bd2af-e8fa-4aa4-aab8-dcef701f4af9"

client <- connect()

content <- content_item(client, CONTENT_GUID)
vanity <- get_vanity_url(content)

The get_vanity_url() function returns a character vector containing the Vanity URL.

> vanity
[1] "/my-vanity-url/"

Discussion

The Connect server also stores the timestamp for when the Vanity URL was created. While the content.vanity attribute is a useful shortcut for obtaining the Content URL path, to view the entire vanity object you must call the content.find_vanity method.

>>> content.find_vanity()
{ "path": "/my-dashboard/", "content_guid": "25438b83-ea6d-4839-ae8e-53c52ac5f9ce", "created_time": "2006-01-02T15:04:05-07:00" }

Note that if a Vanity URL is not assigned to the Content, the server will return a “404 Not Found” HTTP status.

>>> content.find_vanity()
posit.connect.errors.ClientError: {"error_code": 4, "error_message": "The requested object does not exist.", "http_status": 404, "http_message": "Not Found", "payload": null}

See Also