Get a short URL from YOURLS in Zapier

I love automating things. For a few years now I’ve configured my site using Jetpack’s Publicize module in conjunction with Buffer to automatically post new content to my social networks. However, there’s a couple of limitations with that approach:

  1. Publicize seemingly won’t let you use the post’s short URL,
  2. You have little control over when these posts go out on social media.

With my new found love of Zapier, I sought to rectify both of these issues.

First up: getting a YOURLS short URL for a post in Zapier. You can do this using the relatively new Code by Zapier app. This app allows you to run Python or JS to perform calculations, or modify or retrieve data.

I sought to leverage YOURLS’ API, whereby you can make HTTP requests to your YOURLS installation to get short URLs for any URL you provide.

My general process is to check an RSS feed for new entries (I didn’t feel comfortable providing a username and password to Zapier for my WordPress install in order to the use the WordPress app) and then to take the item URL from the RSS feed and send it to YOURLS to give me a short URL back (if a short URL exists it simply returns it: otherwise it creates one and returns it).

Item 1 in my zap is the RSS feed check. Item 2 is the Python code. Here’s what the code generally looks like where article_url is an input created from the item link in Step 1:


request_url = "https://dave.pe/yourls-api.php?signature=xxxxxxxx&action=shorturl&format=json&url={}".format( input['article_url'] )
yourls_response = requests.get(request_url)
if yourls_response.status_code != 200:
status = "fail"
exit()
data = yourls_response.json()
shorturl = data["shorturl"]
return {
'shorturl': shorturl
}

When you run that, you should now have a short URL available to you for use in subsequent steps in your zap.

I then used this in conjunction with the Delay app to create new social posts on a set schedule using my short URL from YOURLS.

Seriously, I can’t get enough of Zapier right now…

By Dave

Dave is the proud father of Ellie and Jack. There's nothing that makes him happier than spending time with his incredible wife and their amazing children. He's a civil/mechanical engineer and he also builds and maintains WordPress websites.

6 comments

  1. Hi,

    Do you think you could also make this work for Airtable? What I wish to do is use Airtable to keep all my campaigns together and once a new value has been added to my airtable field “campaign link” let it shorten automatically and also push back the link to the same row field “short link”.

    Please let me know if you could help with this/ give some tips

  2. That’s awesome! Thank you for sharing this. I took a different approach. I created a zap with one trigger and two actions. Once I post a picture on Instagram, the first action is to call the python code and generate the short URL, and the second action is to post on twitter using the instagram outputs from step 1 and the short_url from step 2. It works like a charm. I’ve tried to do this with IFTTT, but you can only call one action, and I had to mess with YOURLS plugins. I was able to make it work, but it would stop working out of nowhere.

  3. Dave,
    This is fantastic!
    Is there a way to use this to get an alert when someone clicks on a link?
    I have YOURLS on its own domain, not using WordPress. Is it possible to alter this code to work without a WP install?
    Thanks,
    Josh

    1. Hi Josh, and thanks for the kind comments.

      I don’t believe there’s a way to get an alert when someone clicks on a link. You’d have to find (or write) a plugin which could do that for you.

      Also, this code here is written to work inside Zapier. It has no dependency on WordPress: it’s just grabbing information from an RSS feed that happens to be produced by WordPress but could just as easily be created by another piece of software.

  4. Hi Dave,

    First, thank you very much for the contribution, I have used it a lot with success.

    On the other hand do you know if yourls have changed anything? The code stopped working a few days ago, I throw this error: Traceback (most recent call last):

    Runtime.ExitError: RequestId: 343967f0-6cc4-4480-b59f-b00be4edea12 Error: Runtime exited without providing a reason

    Thank you very much for your help

    1. Edwin,

      I don’t know if anything has changed on YOURLS’ end. Did you actually update YOURLS? If not, nothing would have changed. This seems like more of an error on the Zapier side, either that the task is failing, or they updated their software (like Python) which is now incompatible with this code as it is?

      Thanks

Leave a Reply