Skip to content

Commit

Permalink
feat: push notebook using requests
Browse files Browse the repository at this point in the history
FlorentLvr committed Feb 15, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 8733d2d commit 396b6e8
Showing 1 changed file with 59 additions and 34 deletions.
93 changes: 59 additions & 34 deletions HubSpot/HubSpot_Search.ipynb
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "05d1337a-82e4-4dcf-8a20-29129ad7d051",
"id": "d5d6f3cc-2668-4d3c-ad49-63a3fc63455a",
"metadata": {
"papermill": {},
"tags": []
@@ -13,7 +13,7 @@
},
{
"cell_type": "markdown",
"id": "cb4850f4-c9aa-4982-b2c8-bead11f1df62",
"id": "f8978178-7f09-417e-9f0a-3918a4d374b4",
"metadata": {
"papermill": {},
"tags": []
@@ -24,18 +24,18 @@
},
{
"cell_type": "markdown",
"id": "5e8d86cb-2f08-4d7a-806a-51749072b2dd",
"id": "bf232ea3-a970-48c8-b01b-1c7cc5d17cbf",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Tags:** #hubspot #crm #search #endpoints #filter #sort"
"**Tags:** #hubspot #crm #search #endpoints #python #requests"
]
},
{
"cell_type": "markdown",
"id": "594920e8-eec5-4254-a7d3-e444a1662794",
"id": "00ccb408-774b-499e-99d2-bd5ac8e8a37e",
"metadata": {
"papermill": {},
"tags": []
@@ -46,7 +46,7 @@
},
{
"cell_type": "markdown",
"id": "6c6698c2-7ed0-4914-9f0a-2bdbad24a688",
"id": "d7adff63-c4ce-4a7e-9a4c-c1367c2ca859",
"metadata": {
"papermill": {},
"tags": []
@@ -57,29 +57,31 @@
},
{
"cell_type": "markdown",
"id": "45e077f1-c4eb-42fa-84ef-12b622d49597",
"id": "68350cf5-e010-4618-b61e-4f77a4fe03ba",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Description:** This notebook demonstrates how to use the CRM search endpoints to filter, sort, and search objects, records, and engagements across your CRM. For example, it shows how to get a list of contacts in your account, or a list of all open deals."
"**Description:** This notebook demonstrates how to use the CRM search endpoints with python requests to filter, sort, and search objects, records, and engagements across your CRM. For example, it shows how to get a list of contacts in your account, or a list of all open deals."
]
},
{
"cell_type": "markdown",
"id": "400cc518-6a93-4fe9-be45-a9b27302737f",
"id": "c99a7810-dccf-4d25-974f-f81586aa8902",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**References:**\n- [HubSpot API Documentation](https://developers.hubspot.com/docs/api/crm/search)\n- [HubSpot Python Client Library](https://github.com/HubSpot/hubspot-api-python)"
"**References:**\n",
"- [HubSpot API Documentation](https://developers.hubspot.com/docs/api/crm/search)\n",
"- [Python Requests Library](https://docs.python-requests.org/en/latest/)"
]
},
{
"cell_type": "markdown",
"id": "7f3c33e7-67e3-423c-a5e8-1e4273ada372",
"id": "28d0654e-579f-4e9a-9fe5-7234af80dd9a",
"metadata": {
"papermill": {},
"tags": []
@@ -90,7 +92,7 @@
},
{
"cell_type": "markdown",
"id": "0d9d5cf7-5ade-43c2-8094-7f35e210d568",
"id": "c2f9168c-d148-4511-b75f-a57ee0588b2a",
"metadata": {
"papermill": {},
"tags": []
@@ -102,39 +104,47 @@
{
"cell_type": "code",
"execution_count": null,
"id": "a509ad3e-5299-4916-bda9-fe0e062ac9c8",
"id": "34197898-5462-40a5-8bbc-38c91e3cf0fd",
"metadata": {
"papermill": {},
"tags": []
},
"source": "import hubspot\nfrom hubspot import crm",
"outputs": []
"outputs": [],
"source": [
"import requests\n",
"import json"
]
},
{
"cell_type": "markdown",
"id": "f1f138b2-5b75-43f6-a427-fd9884cdc1da",
"id": "a8400c31-af21-4fc7-8cd2-b7bda6d6afe7",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Setup variables\n- `api_key`: Your HubSpot API Key. You can find instructions on how to get it [here](https://knowledge.hubspot.com/integrations/how-do-i-get-my-hubspot-api-key).\n- `object_type`: The type of object you want to search (e.g., 'contacts', 'deals', etc.).\n- `filter_group`: The filter group to apply to the search."
"### Setup variables\n",
"- `API_KEY`: Your HubSpot API Key. You can find instructions on how to get it [here](https://knowledge.hubspot.com/integrations/how-do-i-get-my-hubspot-api-key).\n",
"- `ENDPOINT_URL`: The endpoint URL for the CRM search API. For this example, we will use the contacts search endpoint."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bc4f9760-10a3-4e8f-97ca-e270b430c246",
"id": "b8e252fb-6dd2-42e4-88a5-6ccf2c40072f",
"metadata": {
"papermill": {},
"tags": []
},
"source": "api_key = \"your_api_key\"\nobject_type = \"contacts\"\nfilter_group = {\"propertyName\": \"firstname\", \"operator\": \"EQ\", \"value\": \"John\"}",
"outputs": []
"outputs": [],
"source": [
"API_KEY = \"your-api-key\"\n",
"ENDPOINT_URL = \"https://api.hubapi.com/crm/v3/objects/contacts/search\""
]
},
{
"cell_type": "markdown",
"id": "1437aa25-7284-431c-b83e-b039fcc7b31a",
"id": "a696e461-6282-486f-a2c4-46b5249f47cc",
"metadata": {
"papermill": {},
"tags": []
@@ -145,40 +155,52 @@
},
{
"cell_type": "markdown",
"id": "558db766-27f8-4a62-b081-3d73e272047a",
"id": "cfbae511-389c-4eb1-a89b-e1efa3f1af12",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Search CRM Objects"
"### Search Contacts"
]
},
{
"cell_type": "markdown",
"id": "3667e08d-9d2a-4c3c-9cc5-4f8d4718080f",
"id": "1c7b935a-d37b-4b90-9101-4078573988ff",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"This function uses the HubSpot CRM Search API to search for CRM objects based on the provided filter group. It returns a list of matching objects."
"This function sends a POST request to the HubSpot CRM search endpoint. It uses the provided API key for authentication and sends a JSON payload that specifies the search parameters."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "96f07766-8ffb-4053-81e3-875535553d9d",
"id": "e8d4b220-4a53-4601-9e78-4b40f53c9060",
"metadata": {
"papermill": {},
"tags": []
},
"source": "def search_crm_objects(api_key, object_type, filter_group):\n client = hubspot.Client.create(api_key=api_key)\n search_api = crm.SearchApi(client)\n filter = crm.Filter(**filter_group)\n filter_group = crm.FilterGroup(filters=[filter])\n query = crm.PublicObjectSearchRequest(filter_groups=[filter_group])\n results = search_api.do_search(object_type, public_object_search_request=query)\n return results",
"outputs": []
"outputs": [],
"source": [
"def search_contacts(api_key, endpoint_url, query):\n",
" headers = {\"Content-Type\": \"application/json\", \"Authorization\": f\"Bearer {api_key}\"}\n",
" payload = {\n",
" \"filterGroups\": [\n",
" {\"filters\": [{\"fieldName\": \"firstname\", \"operator\": \"EQ\", \"value\": query}]}\n",
" ],\n",
" \"sort\": [{\"propertyName\": \"createdate\", \"direction\": \"DESCENDING\"}],\n",
" \"properties\": [\"firstname\", \"lastname\", \"email\"],\n",
" }\n",
" response = requests.post(endpoint_url, headers=headers, data=json.dumps(payload))\n",
" return response.json()"
]
},
{
"cell_type": "markdown",
"id": "23d1947f-2102-469f-9355-73120946fce3",
"id": "46a12d9c-6742-4c92-9e24-d91b16fd2eb7",
"metadata": {
"papermill": {},
"tags": []
@@ -189,7 +211,7 @@
},
{
"cell_type": "markdown",
"id": "f938d76f-cd3b-43fe-b520-d937a74ef024",
"id": "7fe1f917-af33-4c29-b12f-6b5ba3dd3b8b",
"metadata": {
"papermill": {},
"tags": []
@@ -201,13 +223,16 @@
{
"cell_type": "code",
"execution_count": null,
"id": "d25c7157-faa1-4ab6-b0f6-a1596ba40a46",
"id": "f1ba4b45-75f7-4072-b887-62031393cc98",
"metadata": {
"papermill": {},
"tags": []
},
"source": "result = search_crm_objects(api_key, object_type, filter_group)\nprint(result)",
"outputs": []
"outputs": [],
"source": [
"result = search_contacts(API_KEY, ENDPOINT_URL, \"John\")\n",
"print(json.dumps(result, indent=4))"
]
}
],
"metadata": {
@@ -238,4 +263,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}

0 comments on commit 396b6e8

Please sign in to comment.