How to create a bunch of Google Alerts, in 3 minutes?
Tags: javascript, google, alerts, automatization
Published: 2019-08-27 16:47:14

---
title: How to create a bunch of Google Alerts, in 3 minutes?
published: true
description: Sometimes you want to create a lot of Google Alerts. Here's how in less than 3 minutes.
tags: #js #google #alerts #automatization
---


[Google Alerts](https://www.google.com/alerts) is a very handy and powerful service to stay informed of what's happening in a particular field. 

I'm a huge fan of this service and I create dozens of new alerts every week. Recently, I wanted to add 100+ Google Alerts to being informed about APIs that we're working with at [Bearer](https://www.bearer.sh).

Unfortunately, Google Alerts doesn't provide an API. And I didn't felt ok to share my Google credentials (email + password) to [the](https://www.npmjs.com/package/google-alerts-api) [available](https://medium.com/t-brian-jones-engineering/google-alerts-api-client-for-creating-alerts-in-bulk-2f20a17f087f) [libraries](https://pypi.org/project/google-alerts/).

## What do we need?

First, a list of keywords that we want to add alerts on:

```js
// Here's my list of keywords to add Google Alerts on;
// Change it with whatever you want to be informed of.
const keywords = ["GitHub API", "Google Alerts API", "Dev.to API"]
```

Then, head to [Google Alerts](https://www.google.com/alerts) so we will learn how it works behind the scene:

![Google Alerts homepage](https://pix.watch/DO6exB/BZ2pVW.png)

I'm using Google Chrome, but that should work just fine with Safari or Firefox.

## Create a Google Alert with JS

On Google Alerts homepage, open the developer tools <kbd>Alt</kbd>+<kbd>Command</kbd>+<kbd>J</kbd> (on Mac) or <kbd>Ctrl</kbd>+<kbd>Shit</kbd>+<kbd>J</kbd> (on Windows), then open the `Network` tab. You should see something like:

![Network request opened on Google Alerts homepage](https://pix.watch/Qt8-II/_LTCvL.png)

Now create a sample alert using `dev.to` as the keyword. The network tab will show a request to the `/create` endpoint. Use `Copy as fetch` to see what's inside that request:

![Request to the /create endpoint](https://pix.watch/noiPEk/xR3iqw.png)

We are almost done 🙌 If you paste that into the console, you will have something like:

```js
// Code has been prettified
fetch(
  "https://www.google.com/alerts/create?x=ABJHsmWAbcU-xxxxxxxxxxxxxxxxxxxxx&hl=us",
  {
    "credentials": "include",
    "headers": {
    "accept": "*/*",
    "accept-language": "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7",
    "cache-control": "no-cache",
    "content-type": "application/x-www-form-urlencoded;charset=UTF-8",
    "pragma": "no-cache",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin",
    "x-client-data": "xxxxxxxxxxxxxxxxxxxxx"
    },
    "referrer": "https://www.google.com/alerts?hl=us",
    "referrerPolicy": "no-referrer-when-downgrade",
    // The dev.to keyword is passed ==================== right here ∨∨∨
    "body": "params=%5Bnull%2C%5Bnull%2Cnull%2Cnull%2C%5Bnull%2C%22dev.to%22%2C%22com%22%2C%5Bnull%2C%22en%22%2C%22US%22%5D%2Cnull%2Cnull%2Cnull%2C0%2C1%5D%2Cnull%2C3%2C%5B%5Bnull%2C1%2C%22corentin%40bearer.sh%22%2C%5Bnull%2Cnull%2C10%5D%2C2%2C%22en-US%22%2Cnull%2Cnull%2Cnull%2Cnull%2Cnull%2C%220%22%2Cnull%2Cnull%2C%22AB2xxxxxxxxxxx%22%5D%5D%5D%5D",
    "method": "POST",
    "mode": "cors"
  }
);

```

As you might see, the `dev.to` keyword is passed into the body. Changing it to something else, will let us automatically add a new Google Alert 🥳 


## A script that creates Google Alerts in bulk


```js
// Replace with your keywords list
var keywords = ["GitHub API", "Google Alerts API", "Dev.to API"]

function addAlert(i) {
  // Retrieve the keyword to work with
  const keyword = encodeURIComponent(keywords[i])

  // Stop the script if there's no keyword
  if (!keywords[i] || !keyword) { return; }

  console.log(`Adding ${keyword}`)

  // 1. Replace the line below with your own fetch (see Copy as fetch above)
  // 2. Replace `dev.to` with `${keyword}`
  fetch(/*...*/)
  
  // Exponentially delay the next request,
  // to avoid rate limit on Google.
  .then(() => { setTimeout(function() {addAlert(i+1)}, (Math.min(i || 2, 30) * 1000)) })
}

addAlert(0)
```

I recommend adding it as a snippet on your Google Chrome ([learn how to do it right here](https://developers.google.com/web/tools/chrome-devtools/javascript/snippets)).