Creating Vanity URLs

Problem

You want to create a Vanity URL for 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 unique identifier (guid). Then assign the Vanity URL path to the content.vanity attribute.

from posit import connect

client = connect.Client()

CONTENT_GUID = "154bd2af-e8fa-4aa4-aab8-dcef701f4af9"
CONTENT_VANITY_PATH = "my-dashboard"

content = client.content.get(CONTENT_GUID)
content.vanity = CONTENT_VANITY_PATH

Once set, view the Content Vanity URL path. Notice that the server has modified your path to include proper path separators.

>>> content.vanity
"/my-dashboard/"
library(connectapi)
client <- connect()

CONTENT_GUID <- "154bd2af-e8fa-4aa4-aab8-dcef701f4af9"
VANITY_URL <- "/my-dashboard/"

# The vanity_is_available() function returns TRUE if vanity_url is available
# on the server.
if (vanity_is_available(client, VANITY_URL)) {
  content <- content_item(client, CONTENT_GUID)
  set_vanity_url(content, VANITY_URL)
}

A successful response prints the Content GUID and Vanity URL.

Posit Connect Content Vanity URL:
  Content GUID: 154bd2af-e8fa-4aa4-aab8-dcef701f4af9
  Vanity URL: /my-dashboard/

If you call set_vanity_url() with an unavailable vanity path, the Connect server will return a “409 Conflict” HTTP status. However, the example above uses the vanity_is_available() function to avoid this.

Error in `self$raise_error()`:
! https://rsc.radixu.com/__api__/v1/content/154bd2af-e8fa-4aa4-aab8-dcef701f4af9/vanity request failed with Client error: (409) Conflict

Discussion

If a Vanity URL path is already assigned to another Content item, the server will return a “409 Conflict” HTTP status.

>>> content.vanity = "path/already/exists"
posit.connect.errors.ClientError: {"error_code": 51, "error_message": "Vanity path conflicts with one or more already in use.", "http_status": 409, "http_message": "Conflict", "payload": null}

To forcibly reassign a Vanity URL that is already assigned, call the content.create_vanity method and set the argument force to True.

Note

This action requires ownerships of both pieces of Content or administrator privileges.

>>> content.create_vanity(path="path/already/exists", force=True)
>>> content.vanity
"/path/already/exists/"