Copy of Snippet "Single use Calendly link generator"

Generate a single use Calendly event invite using a Text Blaze snippet

{note}
Summary: This snippet generates a single use Calendly link, which you can then send via email.

Check event slug
{required_event_slug="15min"}
You are generating a single-use link for event slug {=required_event_slug}. Please make sure the spelling and capitalization in this slug are correct.

Getting your Calendly API token
Follow the steps described on this link to generate an API token.
You can use the same token each time you schedule a call.
[Instead of using the formfield below, you can also store the token into the snippet but its not recommended]
{formtext: name=apikey}

{if: apikey <> ""}
Scheduling the meet
We follow the API documentation on this link.
{urlload: https://api.calendly.com/users/me; method=GET; done=(res) -> ["response"=catch(fromJSON(res), "???")]; headers=Authorization=Bearer {=apikey}}
{user_uri=response["resource"]["uri"]}
{urlload: https://api.calendly.com/event_types?user={=user_uri}; method=GET; done=(res) -> ["response2"=catch(fromJSON(res), "???")]; headers=Authorization=Bearer {=apikey}}

{required_event=filter(response2["collection"], (x) -> x["slug"]==required_event_slug)[1]}
{event_uri=required_event["uri"]}
Your user URI is {=user_uri}
Your event URI is {=event_uri}
We made two API calls to extract this. You can store this value directly in the snippet so that we don't have to make the two API calls each time.

{urlload: https://api.calendly.com/scheduling_links?owner={=event_uri}&owner_type="EventType"&max_event_count=1; method=POST; done=(res) -> ["response3"=catch(fromJSON(res), "???")]; headers=Authorization=Bearer {=apikey}}
{booking_url=response3["resource"]["booking_url"]}
{else}
Booking URL is generated after you enter the API key.
{endif}
{endnote}
Congratulations, your booking URL is {=booking_url}