<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Albert Soriano</title>
    <description>The latest articles on DEV Community by Albert Soriano (@albertsg).</description>
    <link>https://dev.to/albertsg</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F688790%2F43714c64-26cd-4b4a-8bfe-11a5c8080534.jpeg</url>
      <title>DEV Community: Albert Soriano</title>
      <link>https://dev.to/albertsg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/albertsg"/>
    <language>en</language>
    <item>
      <title>How to create a list of random articles in Hubspot</title>
      <dc:creator>Albert Soriano</dc:creator>
      <pubDate>Thu, 24 Mar 2022 15:53:10 +0000</pubDate>
      <link>https://dev.to/albertsg/how-to-create-a-list-of-random-articles-in-hubspot-4pee</link>
      <guid>https://dev.to/albertsg/how-to-create-a-list-of-random-articles-in-hubspot-4pee</guid>
      <description>&lt;p&gt;If you are developing a new Blog using Hubspot CMS, you will probably need to create a blog listing page. While the "default" behaviour of these listing pages is to display the latest articles in order, you can actually play around and create different listing "logic". &lt;/p&gt;

&lt;p&gt;In this article I would like to show you how to create a listing of random articles in Hubspot. This could be useful if you want to have some kind of blog collection somewhere in your page to display different articles for each page visit. &lt;/p&gt;

&lt;p&gt;First of all, we need to get our blog posts. To achieve this, we could use the following two functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developers.hubspot.com/docs/cms/hubl/functions#blog-recent-posts"&gt;blog_recent_posts()&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developers.hubspot.com/docs/cms/hubl/functions#blog-popular-posts"&gt;blog_popular_posts()&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To be honest, since we will be showing random posts, it doesn't really matter which function we use. Just keep in mind the difference between them in case you want to add some extra logic to this "randomness". Also, remember these functions will return maximum 200 articles.&lt;/p&gt;

&lt;p&gt;Let's go ahead and set a variable &lt;strong&gt;&lt;em&gt;articles&lt;/em&gt;&lt;/strong&gt; that will include 30 blog posts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{% set articles = blog_recent_posts("default", 30) %}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now it's time to loop through our articles.&lt;br&gt;
&lt;strong&gt;But here's the catch!&lt;/strong&gt; Instead of looping through our article's array as it is, we will use the &lt;a href="https://developers.hubspot.com/docs/cms/hubl/filters#shuffle"&gt;&lt;strong&gt;&lt;em&gt;shuffle&lt;/em&gt;&lt;/strong&gt; filter&lt;/a&gt; to randomize the order of iteration through the sequence.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{% set articles = blog_recent_posts("default", 30) %}

{% for article in articles|shuffle %}
    &amp;lt;a href="{{ article.absolute_url }}"&amp;gt;{{ article.name }}&amp;lt;/a&amp;gt;
{% endfor %}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The previous code will display all our 30 articles in random order, but what if we want to display only 5 articles for example?&lt;/p&gt;

&lt;p&gt;In this case, we will use the variable &lt;strong&gt;&lt;em&gt;loop.index&lt;/em&gt;&lt;/strong&gt; to make sure we only display content during the 5 first loop iterations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{% set articles = blog_recent_posts("default", 30) %}

{% for article in articles|shuffle %}
    {% if loop.index &amp;lt;= 5 %}
        &amp;lt;a href="{{ article.absolute_url }}"&amp;gt;{{ article.name }}&amp;lt;/a&amp;gt;
    {% endif %}
{% endfor %}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it! Easy, right?&lt;/p&gt;

&lt;p&gt;Here are some &lt;strong&gt;takeaways&lt;/strong&gt; you should keep in mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Always get more articles in your &lt;em&gt;articles&lt;/em&gt; variable than the number of articles you want to display. Otherwise you would always show the same content.&lt;/li&gt;
&lt;li&gt;If you want to use this random functionality in your main listing page, you can use the variable &lt;strong&gt;&lt;em&gt;contents&lt;/em&gt;&lt;/strong&gt; instead of getting articles using the two functions mentioned before.&lt;/li&gt;
&lt;li&gt;Remember to read &lt;a href="https://developers.hubspot.com/docs/cms/key-concepts"&gt;Hubspot's documentation&lt;/a&gt; when using a function or filter as some of them have limitations.&lt;/li&gt;
&lt;li&gt;The filter &lt;strong&gt;|shuffle&lt;/strong&gt; will affect your &lt;a href="https://developers.hubspot.com/docs/cms/developer-reference/cdn/prerendering#hubl-filters"&gt;prerendering&lt;/a&gt;. To avoid it, it's recommended to use Javascript.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>hubspot</category>
      <category>tutorial</category>
      <category>html</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to have multiple Blog post templates in Hubspot</title>
      <dc:creator>Albert Soriano</dc:creator>
      <pubDate>Sun, 19 Dec 2021 20:32:00 +0000</pubDate>
      <link>https://dev.to/albertsg/having-multiple-blog-post-templates-in-hubspot-10i1</link>
      <guid>https://dev.to/albertsg/having-multiple-blog-post-templates-in-hubspot-10i1</guid>
      <description>&lt;p&gt;&lt;strong&gt;2025 UPDATE&lt;/strong&gt;: I've simplified this solution! To see the updated version, please visit the &lt;a href="https://7bits.dev/blog/how-to-use-multiple-blog-post-templates-in-hubspot-with-custom-modules" rel="noopener noreferrer"&gt;7bits blog&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;If you want to create a Blog in Hubspot, you will notice that it requires you to have two templates, one for listing pages and the other one for blog posts.&lt;/p&gt;

&lt;p&gt;While it is great to have two templates for different types of pages, what if you want to have more than one template for your blog posts?&lt;/p&gt;

&lt;p&gt;Imagine you want to have two different types of blog posts, one for company related news and another one for industry related news. And now imagine you want to have a different template for each type, so they look a bit different and include different elements.&lt;/p&gt;

&lt;p&gt;While Hubspot doesn't allow you to choose the template for each blog post, this is something we can achieve using custom modules.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a template selector custom module
&lt;/h2&gt;

&lt;p&gt;The first thing you need to do is create a module that will allow you to select what blog post template you want. &lt;/p&gt;

&lt;p&gt;This will allow content authors to easily select the template they want for their blog posts, all they will need to do is select the template from a dropdown list that will appear in the modules sidebar.&lt;/p&gt;

&lt;p&gt;There are different ways to create a module, but we will do it through the Design Manager as I think it's the easiest way to do it.&lt;/p&gt;

&lt;p&gt;Go to the Design Manager, find/create a folder where you want to store your modules, right click on it and create a new file. Then select Module.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyzesvt1jwo34h3c4e5yx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyzesvt1jwo34h3c4e5yx.png" alt="Module creation" width="800" height="660"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's important to note that we will need a Local Module because the module's value has to be different for each blog post (or landing page as this can be applied to landing pages as well if needed). Your module settings should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqn97u4gjo4hr7946rbyo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqn97u4gjo4hr7946rbyo.png" alt="Module settings" width="800" height="828"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you've created the module, we will add the options for all the blog templates you want to have in your page.&lt;/p&gt;

&lt;p&gt;To be honest, we just need one field here, which in our case will be a Choice field as in my opinion is the clearest way for content authors to choose the blog template.&lt;/p&gt;

&lt;p&gt;Let's keep it simple by adding just 2 template options. You can modify this module to add as many template options as you want, together with more module options if needed.&lt;/p&gt;

&lt;p&gt;I will call the module "Blog template selector" (blog_template_selector) and it looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpj68wmfdhzf7dvf7c408.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpj68wmfdhzf7dvf7c408.png" alt="Hubspot module" width="702" height="738"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpw0ncfkfthsn8g8bqzl9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpw0ncfkfthsn8g8bqzl9.png" alt="Hubspot module" width="692" height="532"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flkyu8if1hikyd0zefqzw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flkyu8if1hikyd0zefqzw.png" alt="Hubspot module" width="688" height="622"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since we will use this module just for "logic", we will not add any HTML, CSS or JS to it.&lt;/p&gt;
&lt;h2&gt;
  
  
  Create your main and partial templates##
&lt;/h2&gt;

&lt;p&gt;The template logic will work like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We will have a main template for Blog posts that will automatically be assigned to each new article. This template will include our template selector module.&lt;/li&gt;
&lt;li&gt;We will have two (or as many as needed) partial templates that will be loaded into the main template according to the selected template in the module.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To visually explain all of this, I've created a main global template (main_template.html) and two partial templates (blog_template_one.html &amp;amp; blog_template_two.html).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl7ml6hu8tb8tm5dm5ujt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl7ml6hu8tb8tm5dm5ujt.png" alt="Folder structure" width="610" height="328"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Add the template selector to your template
&lt;/h2&gt;

&lt;p&gt;Our next step is to add the module we've created to our main template (main_template.html). &lt;br&gt;
In order to add a module to a template, go to the module's options, scroll to the bottom and copy the code in Template Usage section. It should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{% module "module_16399418628793" path="/blog_template_selector/blog_template_selector", label="blog_template_selector" %}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next step is to add this code to our main template.&lt;/p&gt;

&lt;h2&gt;
  
  
  Load partial templates based on the module
&lt;/h2&gt;

&lt;p&gt;Now that our module is added to the main template, it's time to add the logic that will load our partial templates.&lt;/p&gt;

&lt;p&gt;This logic is as simple as an IF statement. Our main template should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!--
    templateType: blog
    isAvailableForNewContent: true
--&amp;gt;
{% module "blog_template_selector" path="/blog_template_selector/blog_template_selector", label="blog_template_selector", export_to_template_context=True %}


&amp;lt;!doctype html&amp;gt;
&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="utf-8"&amp;gt;
    &amp;lt;title&amp;gt;{{ content.html_title }}&amp;lt;/title&amp;gt;
    &amp;lt;meta name="description" content="{{ content.meta_description }}"&amp;gt;
    {{ standard_header_includes }}
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    {% if widget_data.blog_template_selector.template == 'template_1' %}
      {% include '/blog_template_selector/blog_template_one.html' %}
    {% else %}
      {% include '/blog_template_selector/blog_template_two.html' %}
    {% endif %}
    {{ standard_footer_includes }}
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some important things to mention here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It's important that your templateType is 'blog'&lt;/li&gt;
&lt;li&gt;Make sure your module has the &lt;strong&gt;export_to_template_context=True&lt;/strong&gt; field&lt;/li&gt;
&lt;li&gt;Since 'module_16399418628793' is not an ideal name for a module in our code, I've renamed it to 'blog_template_selector'&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Select the template for your blog post
&lt;/h2&gt;

&lt;p&gt;Everything should be ready! Now content authors should be able to select the template while editing an article. It's as simple as selecting the template in our module as you can see below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flb8jk2obp7gq70mfvogm.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flb8jk2obp7gq70mfvogm.gif" alt="Template selection" width="600" height="259"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>hubspot</category>
      <category>html</category>
      <category>hubl</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Update tags using Hubspot API</title>
      <dc:creator>Albert Soriano</dc:creator>
      <pubDate>Thu, 19 Aug 2021 10:13:35 +0000</pubDate>
      <link>https://dev.to/albertsg/update-tags-using-hubspot-api-4bap</link>
      <guid>https://dev.to/albertsg/update-tags-using-hubspot-api-4bap</guid>
      <description>&lt;p&gt;Hello Dev.to community!&lt;/p&gt;

&lt;p&gt;My name is Albert Soriano, I'm a Web Developer at Avast Software and this is my first Dev.to post. I hope some of you find it useful! &lt;/p&gt;

&lt;p&gt;Today I'm going to talk about &lt;strong&gt;Hubspot tags&lt;/strong&gt; and how to &lt;strong&gt;automatically tag articles&lt;/strong&gt; by using Hubspot's API.&lt;/p&gt;

&lt;p&gt;For those who don't know what Hubspot tags are, here's what the documentation says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Tags allow you to manage your blog posts by subject. Once you've added tags to your posts, you can customize your templates to link to a feed of all posts by tag. You can also add a post filter module so visitors can filter posts by tag.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Adding tags is usually a &lt;strong&gt;manual process&lt;/strong&gt; where content authors manually tag articles with a tag or with multiple tags. It's a very simple process in Hubspot, but what if you need to tag several articles at once? &lt;/p&gt;

&lt;p&gt;Tagging hundreds of articles manually is extremely time consuming, but luckily we can use &lt;strong&gt;Hubspot's API to help us tagging articles automatically&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The first thing you need to do is find the &lt;strong&gt;ID of your new tag&lt;/strong&gt;. This is a bit tricky since Hubspot doesn't provide a clear way to get the tag ID, but you can find it using the API to list all your tags and their information (name, ID, etc.). &lt;/p&gt;

&lt;p&gt;To get the tag ID we will use Hubspot's &lt;strong&gt;API test calls&lt;/strong&gt; to retrieve data from our tags.&lt;/p&gt;

&lt;h3&gt;
  
  
  Go to the API documentation and run a test call
&lt;/h3&gt;

&lt;p&gt;Test calls will require your API key. To get the API key, go to Hubspot, Settings, API key.&lt;/p&gt;

&lt;p&gt;Now that you have the API key, go to Hubspot's API documentation (&lt;a href="https://developers.hubspot.com/docs/api/cms/blog-tags#endpoint?spec=GET-/cms/v3/blogs/tags" rel="noopener noreferrer"&gt;here&lt;/a&gt;), find the "Get all Blog Tags" function and run it. &lt;/p&gt;

&lt;p&gt;The response should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz074p36c84hzx9s69qx2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz074p36c84hzx9s69qx2.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All you have to do is find your tag and &lt;strong&gt;copy the ID&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Retrieve all blog posts from your Blog
&lt;/h3&gt;

&lt;p&gt;There are many ways to approach this, but our second step in this process will be to &lt;strong&gt;retrieve all blog posts from our Blog&lt;/strong&gt; and store them in an array. We will then update the tags of each blog and will call the API to update the information in Hubspot. &lt;/p&gt;

&lt;p&gt;To retrieve all blog posts and store them in our code, we will use the following code (all code can be found in &lt;a href="https://github.com/albertsg/hubspot-api-scripts/blob/master/scripts/tag-update.js" rel="noopener noreferrer"&gt;this repository&lt;/a&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;callApi&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;


  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GET&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api.hubapi.com/cms/v3/blogs/posts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;qs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;hapikey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;accountKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;application/json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;


  &lt;span class="nf"&gt;request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;posts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;results&lt;/span&gt;

    &lt;span class="nx"&gt;isEnd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;

    &lt;span class="nx"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;offset&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
      &lt;span class="c1"&gt;//Add here any filtering options you want. For example, now I select the blogs to update based on the URL as I have different domains in my account. Feel free to change this logic to something that suits better your needs&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;domainURL&lt;/span&gt;&lt;span class="p"&gt;)){&lt;/span&gt;
        &lt;span class="nx"&gt;blogs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="nf"&gt;checkIfFurtherNeeded&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The previous code will store our all blog posts into the &lt;em&gt;blogs&lt;/em&gt; array. Then it will run the function &lt;em&gt;checkIfFurtherNeeded()&lt;/em&gt; that will check if there are still blogs to process or if it can start updating tags.&lt;/p&gt;

&lt;p&gt;It's important to mention that in this example I'm filtering the blogs by URL as I have different domains in my account. The API will return &lt;strong&gt;all&lt;/strong&gt; blog posts in my account, but it doesn't mean that I have to update them all, I can &lt;strong&gt;filter&lt;/strong&gt; the blogs according to some logic and update only the ones I need.&lt;/p&gt;
&lt;h3&gt;
  
  
  Update tags in blog posts
&lt;/h3&gt;

&lt;p&gt;Once we have all our blog posts, it's time to add the &lt;strong&gt;new tag to the tag list&lt;/strong&gt;. To do that, we can use the following code:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * updateHubTags Evaluates is a blog post includes the tag to be added and calls the function to do it
 */&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;updateHubTags&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;blogs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tagIds&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newTagId&lt;/span&gt;&lt;span class="p"&gt;)){&lt;/span&gt;
      &lt;span class="nc"&gt;UpdateTag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; 
  &lt;span class="p"&gt;})&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="cm"&gt;/**
 * UpdateTag Executes a call to Hubspot's API en processes the data
 * @param  {Object} blog This object includes the information of the blog post where we will add the tag
 */&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;UpdateTag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;

  &lt;span class="c1"&gt;//Update a blog post&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;request&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tagIds&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newTagId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//Add new tag to the array of tag&lt;/span&gt;

  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;PATCH&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`https://api.hubapi.com/cms/v3/blogs/posts/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;qs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;hapikey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;accountKey&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;application/json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;content-type&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;application/json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;tagIds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tagIds&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;json&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="nf"&gt;request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Article &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;htmlTitle&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; updated`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;And that's all! With the code above you will be able to automatically tag your blog posts with a specific tag, saving a lot of time to content authors.&lt;/p&gt;

&lt;p&gt;Here are some things to keep in mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The variable &lt;em&gt;accountKey&lt;/em&gt; should include your API key.&lt;/li&gt;
&lt;li&gt;Keep in mind that Hubspot's requests return only 300 elements (but code above solves it by doing a request several times until all blog posts are obtained).&lt;/li&gt;
&lt;li&gt;You can filter the blogs as you want. In the example above I filter the blogs by URL because I have different domains in the same account.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All the code can be found in my Github account:&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/albertsg" rel="noopener noreferrer"&gt;
        albertsg
      &lt;/a&gt; / &lt;a href="https://github.com/albertsg/hubspot-api-scripts" rel="noopener noreferrer"&gt;
        hubspot-api-scripts
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      JS scripts to perform different operations in Hubspot using their API
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;Hubspot API scripts&lt;/h1&gt;
&lt;/div&gt;

&lt;p&gt;The scripts contained in the &lt;i&gt;scripts&lt;/i&gt; folder will help you doing different tasks with Hubspot's API&lt;/p&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Requirements&lt;/h2&gt;
&lt;/div&gt;

&lt;p&gt;To be able to use the script, you will need:&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt;NodeJS installed. You can install it from &lt;a href="https://nodejs.org/es/" rel="nofollow noopener noreferrer"&gt;this link&lt;/a&gt;.&lt;/li&gt;
    &lt;li&gt;A &lt;a href="https://www.hubspot.com/" rel="nofollow noopener noreferrer"&gt;Hubspot&lt;/a&gt; account.&lt;/li&gt;
    &lt;li&gt;An API key from your Hubspot account. Follow &lt;a href="https://knowledge.hubspot.com/integrations/how-do-i-get-my-hubspot-api-key" rel="nofollow noopener noreferrer"&gt;this link&lt;/a&gt; for more information.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;How to run a script&lt;/h2&gt;
&lt;/div&gt;

&lt;p&gt;In order to run a script, access to your terminal and run &lt;code&gt;node script-name.js&lt;/code&gt;.
Please note that the script might take some time to run, depending on the number of calls and data required.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Scripts&lt;/h2&gt;

&lt;/div&gt;
&lt;div class="markdown-heading"&gt;
&lt;h3 class="heading-element"&gt;Add a tag to a blog post&lt;/h3&gt;

&lt;/div&gt;
If you need to add a tag to multiple blog post, you can use the script &lt;a href="https://github.com/albertsg/hubspot-api-scriptsscripts/tag-update.js" rel="noopener noreferrer"&gt;tag-update.js&lt;/a&gt;.
&lt;p&gt;This script will call the API as many times as needed to retireve ALL blog posts from your account (API is limited to 300 posts per call) and…&lt;/p&gt;
&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/albertsg/hubspot-api-scripts" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Feel free to contribute to the code, I'm sure it can be optimized in many different ways! &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>hubl</category>
      <category>hubspot</category>
      <category>api</category>
    </item>
  </channel>
</rss>
