Creating Vanity URLs
Problem
You want to create a Vanity URL for your Content.
Solution
Requires posit-sdk>=0.6.0
Obtain the content object for your Content. In this example, we get the Content using its 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_PATHOnce 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/
The vanity_is_available() check avoids the “409 Conflict” error described in Reassigning a Vanity URL that is already in use.
Reassigning a Vanity URL that is already in use
If another Content item already uses the Vanity URL path, the server returns a “409 Conflict” HTTP status.
The SDK raises a ClientError.
>>> 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 another Content item uses, call the content.create_vanity method and set the argument force to True.
This action requires ownership of both pieces of Content or administrator privileges.
>>> content.create_vanity(path="path/already/exists", force=True)
>>> content.vanity
"/path/already/exists/"The set_vanity_url() function raises an error.
> set_vanity_url(content, "/path/already/exists/")
Error in `self$raise_error()`:
! https://connect.example.com/__api__/v1/content/154bd2af-e8fa-4aa4-aab8-dcef701f4af9/vanity request failed with Client error: (409) Conflict
To forcibly reassign a Vanity URL that another Content item uses, call set_vanity_url() and set the argument force to TRUE.
This action requires ownership of both pieces of Content or administrator privileges.
set_vanity_url(content, "/path/already/exists/", force = TRUE)Posit Connect Content Vanity URL:
Content GUID: 154bd2af-e8fa-4aa4-aab8-dcef701f4af9
Vanity URL: /path/already/exists/