Skip to content

Commit

Permalink
feat: add auth0 to hubspot project (#133)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Abraham <[email protected]>
  • Loading branch information
pashafateev and daabr authored Dec 19, 2024
1 parent e7efe93 commit 9b86812
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 0 deletions.
78 changes: 78 additions & 0 deletions auth0_to_hubspot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
title: Copy Auth0 Users to HubSpot
description: Periodically add new Auth0 users to HubSpot as contacts
integrations: ["auth0", "hubspot"]
categories: ["Office Automation"]
---

# Auth0 to HubSpot

This project adds new Auth0 users to HubSpot as contacts, on a recurring basis.

## How It Works

1. Fetch new Auth0 users from the last `HOURS` hours
2. Create HubSpot contacts for each user

## Deployment & Configuration

### Cloud Usage (Recommended)

1. Initialize your connections through the UI
2. (Optional) Configure the `HOURS` variable to set the time range for new users to fetch.

> [!NOTE]
> To sync the schedule with the lookup time, update the schedule in the “TRIGGERS” tab. The default "Cron expression" is `@every 24h`.
3. Deploy the project

## Trigger Workflow

The workflow is triggered automatically every `HOURS` hours.

> [!TIP]
> The workflow can also be triggered manually by clicking the "Run" button in the UI. Make sure to set the `check_for_new_users` function as the entrypoint.
> [!IMPORTANT]
> Ensure all connections (Auth0 and HubSpot) are properly initialized before the workflow starts running.
## Self-Hosted Deployment

- [Install AutoKitteh](https://docs.autokitteh.com/get_started/install)
- Set up required integrations:
- [Auth0](https://docs.autokitteh.com/integrations/auth0)
- [HubSpot](https://docs.autokitteh.com/integrations/hubspot)

#### Installation Steps

1. Clone the repository:
```shell
git clone https://github.com/autokitteh/kittehub.git
cd kittehub/auth0_to_hubspot
```

2. Start the AutoKitteh server:
```shell
ak up --mode dev
```

3. Deploy the project:
```shell
ak deploy --manifest autokitteh.yaml
```

The output will show your connection IDs, which you'll need for the next step. Look for lines like:
```shell
[exec] create_connection "auth0_to_hubspot/auth0_connection": con_01j36p9gj6e2nt87p9vap6rbmz created
[exec] create_connection "auth0_to_hubspot/hubspot_connection": con_01j36p9gj6e2nt87p9vap6rbmz created
```

In this example, `con_01j36p9gj6e2nt87p9vap6rbmz` is the connection ID.

4. Initialize your connections using the CLI:
```shell
ak connection init auth0_connection <connection ID>
ak connection init hubspot_connection <connection ID>
```

> [!TIP]
> You can also initialize your connections through the UI.
20 changes: 20 additions & 0 deletions auth0_to_hubspot/autokitteh.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This YAML file is a declarative manifest that describes the setup
# of an AutoKitteh project that adds new Auth0 users to HubSpot as contacts.

version: v1

project:
name: auth0_to_hubspot
vars:
- name: HOURS
value: 24
connections:
- name: auth0_conn
integration: auth0
- name: hubspot_conn
integration: hubspot
triggers:
- name: daily
schedule: "@every 24h"
call: program.py:check_for_new_users

51 changes: 51 additions & 0 deletions auth0_to_hubspot/program.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""This program adds new Auth0 users to HubSpot as contacts."""

import datetime
import os

from autokitteh.auth0 import auth0_client
from autokitteh.hubspot import hubspot_client
from hubspot.crm.contacts import SimplePublicObjectInput

LOOKUP_HOURS = int(os.getenv("HOURS"))

auth0 = auth0_client("auth0_conn")
hubspot = hubspot_client("hubspot_conn")


def check_for_new_users(event):
"""Workflow entrypoint.
Looks up new Auth0 users in the last `HOURS` hours and adds them to HubSpot as contacts.
"""
start, end = _get_time_range(LOOKUP_HOURS)
query = f"created_at:[{start} TO {end}]"

response = auth0.users.list(q=query, search_engine="v3")
add_new_users(response["users"])


def _get_time_range(hours):
"""Calculate start and end times for user lookup."""
now = datetime.datetime.utcnow()
start_time = now - datetime.timedelta(hours=hours)

return (start_time.isoformat() + "Z", now.isoformat() + "Z")


def add_new_users(users):
"""Add new Auth0 users to HubSpot as contacts."""
for user in users:
contact = _create_hubspot_contact(user)
hubspot.crm.contacts.basic_api.create(contact)
print(f"Added {user['email']} to HubSpot")


def _create_hubspot_contact(user):
"""Convert Auth0 user data to HubSpot contact format."""
user_data = {
"email": user["email"],
"firstname": user["given_name"],
"lastname": user["family_name"],
}
return SimplePublicObjectInput(properties=user_data)

0 comments on commit 9b86812

Please sign in to comment.