Set item values in monday.com from external API


Get Started

Not the template you're looking for? Browse more.

About the template


This template demonstrates how to build an integration between monday.com and an external service by pulling weather data from an external API and storing its data in board item when the item column changes. Get started to learn more.

About ScriptRunner Connect


What is ScriptRunner Connect?

ScriptRunner Connect is an AI assisted code-first (JavaScript/TypeScript) integration platform (iPaaS) for building complex integrations and automations.

Can I try it out for free?

Yes. ScriptRunner Connect comes with a forever free tier.

Can I customize the integration logic?

Absolutely. The main value proposition of ScriptRunner Connect is that you'll get full access to the code that is powering the integration, which means you can make any changes to the the integration logic yourself.

Can I change the integration to communicate with additional apps?

Yes. Since ScriptRunner Connect specializes in enabling complex integrations, you can easily change the integration logic to connect to as many additional apps as you need, no limitations.

What if I don't feel comfortable making changes to the code?

First you can try out our AI assistant which can help you understand what the code does, and also help you make changes to the code. Alternatively you can hire our professionals to make the changes you need or build new integrations from scratch.

Do I have to host it myself?

No. ScriptRunner Connect is a fully managed SaaS (Software-as-a-Service) product.

What about security?

ScriptRunner Connect is ISO 27001 and SOC 2 certified. Learn more about our security.

Template Content


README

Scripts

TypeScriptOnMondayWhenAnyColumnChanges
When any column changes

README


๐Ÿ“‹ Overview

This template demonstrates how to build an integration between monday.com and an external service by pulling weather data from an external API and storing its data in board item when the item column changes.

๐Ÿ–Š๏ธ Setup

  • Configure the API Connection and Event Listener by creating a connector for monday.com, or use a existing one.
  • Navigate to Parameters and change the LOCATION_COLUMN_LABEL, TEMPERATURE_COLUMN_LABEL, and WEATHER_API_KEY parameters to appropriate values for your instance.
  • The value needed for WEATHER_API_KEY can be obtained from https://www.tomorrow.io/ by signing up to a free account. Your API key will be presented at the top of your home screen once logged in.

๐Ÿš€ Usage

Update the location column in a monday.com board; after successful event processing, a new temperature value should be set in the temperature column.

API Connections


TypeScriptOnMondayWhenAnyColumnChanges

import { ItemFields, SelectItemReturnType } from '@managed-api/monday-core/definitions/item';
import { GetItemsRequest, GetItemsResponseOK } from '@managed-api/monday-core/types/item';
import { UpdateColumnValueEvent } from '@sr-connect/monday/events';
import Monday from './api/monday';

/**
 * Entry point to "when any column changes" event is triggered
 *
 * @param event Object that holds When any column changes event data
 * @param context Object that holds function invocation context data
 */
export default async function (event: UpdateColumnValueEvent, context: Context): Promise<void> {
    if (context.triggerType === 'MANUAL') {
        console.error(
            'This script is designed to be triggered externally or manually from the Event Listener. Please consider using Event Listener Test Event Payload if you need to trigger this script manually.'
        );
        return;
    }

    // Extract parameters from the event
    const { pulseId, boardId, columnTitle } = event.event;

    // Check if column changes is related to Location column
    if (columnTitle !== context.environment.vars.LOCATION_COLUMN_LABEL) {
        return;
    }

    // Check if the necessary parameters are present in the event
    if (!pulseId) {
        throw Error('Pulse ID not present in the event');
    }
    if (!boardId) {
        throw Error('Board ID not present in the event');
    }

    // Get item from monday.com
    const item = await getItem(pulseId);

    const temperatureColumnId = extractTemperatureColumnId(item.data.items[0], context.environment.vars.TEMPERATURE_COLUMN_LABEL);
    const location = extractLocationValue(item.data.items[0], context.environment.vars.LOCATION_COLUMN_LABEL);

    // Get temperature based location value address
    const weather = await fetchWeather(
        context.environment.vars.WEATHER_API_URL,
        location,
        context.environment.vars.WEATHER_API_KEY
    );

    // Update weather column value with temperature returned from the weather API
    await Monday.Column.changeColumnValue({
        args: {
            board_id: boardId,
            item_id: pulseId,
            column_id: temperatureColumnId,
            value: weather.data.values.temperature.toString()
        },
        fields: {
            id: true
        }
    });
}

// Function to extract the temperature column ID, based on the column label
function extractTemperatureColumnId(item: SelectItemReturnType<ItemFields>, temperatureColumnLabel: string): string {
    const temperatureColumn = item.column_values.find((curr) => curr.column.title === temperatureColumnLabel);

    if (!temperatureColumn) {
        throw Error('Temperature column not found');
    }

    return temperatureColumn.id;
}

// Function to extract and parse the location information from the input row, based on the column label
function extractLocationValue(item: SelectItemReturnType<ItemFields>, locationColumnLabel: string): string {
    const locationColumn = item.column_values.find((curr) => curr.column.title === locationColumnLabel);

    if (!locationColumn) {
        throw Error('Location column not found or column is not type Location');
    }

    return JSON.parse(locationColumn.value);
}

// Function that fetchs data from weather API
// Using the API defined in WEATHER_API_URL and the response object defined in WeatherApiResponse
async function fetchWeather(weatherEndpoint: string, address: string, apiKey: string): Promise<WeatherApiResponse> {
    const response = await fetch(`${weatherEndpoint}?location=${address}&apikey=${apiKey}`);

    if (!response.ok) {
        throw Error(`Failed to fetch weather data: ${response.status} - ${await response.text()}`);
    }

    return response.json<WeatherApiResponse>();
}

// Function that gets an item in a board from monday.com
export async function getItem(itemId: number): Promise<GetItemsResponseOK> {
    // As an example, we're constructing the request object first
    const request: GetItemsRequest = {
        args: {
            ids: [itemId]
        },
        fields: {
            id: true,
            column_values: {
                fields: {
                    id: true,
                    value: true,
                    column: {
                        fields: {
                            title: true
                        }
                    }
                }
            }
        }
    };
    return await Monday.Item.getItems(request);
}

// Weather API response interface
// API reference document: https://docs.tomorrow.io/reference/realtime-weather
interface WeatherApiResponse {
    data: {
        time: string;
        values: {
            cloudBase: number;
            cloudCeiling: number;
            cloudCover: number;
            dewPoint: number;
            freezingRainIntensity: number;
            humidity: number;
            precipitationProbability: number;
            pressureSurfaceLevel: number;
            rainIntensity: number;
            sleetIntensity: number;
            snowIntensity: number;
            temperature: number;
            temperatureApparent: number;
            uvHealthConcern: number;
            uvIndex: number;
            visibility: number;
            weatherCode: number;
            windDirection: number;
            windGust: number;
            windSpeed: number;
        };
    };
    location: {
        lat: number;
        lon: number;
        name: string;
        type: string;
    };
}
Documentation ยท Support ยท Suggestions & feature requests