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 it’s unique identifier (guid). Then assign the Vanity URL path to the content.vanity
attribute.
from posit import connect
= connect.Client()
client
= "154bd2af-e8fa-4aa4-aab8-dcef701f4af9"
CONTENT_GUID = "my-dashboard"
CONTENT_VANITY_PATH
= client.content.get(CONTENT_GUID)
content = CONTENT_VANITY_PATH content.vanity
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)
<- connect()
client
<- "154bd2af-e8fa-4aa4-aab8-dcef701f4af9"
CONTENT_GUID <- "/my-dashboard/"
VANITY_URL
# The vanity_is_available() function returns TRUE if vanity_url is available
# on the server.
if (vanity_is_available(client, VANITY_URL)) {
<- content_item(client, CONTENT_GUID)
content set_vanity_url(content, VANITY_URL)
}
A successful response prints the Content GUID and Vanity URL.
:
Posit Connect Content Vanity URL: 154bd2af-e8fa-4aa4-aab8-dcef701f4af9
Content GUID: /my-dashboard/ Vanity URL
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.
in `self$raise_error()`:
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"
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} posit.
To forcibly reassign a Vanity URL that is already assigned, call the content.create_vanity
method and set the argument force
to True
.
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/"