Skip to content

Instantly share code, notes, and snippets.

@gitfvb
Created June 9, 2020 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gitfvb/ae2ba9b193bb9cf0c4d62756dc3e22da to your computer and use it in GitHub Desktop.
Save gitfvb/ae2ba9b193bb9cf0c4d62756dc3e22da to your computer and use it in GitHub Desktop.
Huspot outlines in PowerShell

Replace Put Get-Unixtime.ps1 in subfolder

# current unixtimestamp with the optional milliseconds
Function Get-Unixtime {
param(
[Parameter(Mandatory=$false)][switch] $inMilliseconds = $false
,[Parameter(Mandatory=$false)][DateTime] $timestamp = ( Get-Date )
)
$multiplier = 1
if ( $inMilliseconds ) {
$multiplier = 1000
}
[long]$unixtime = [double]::Parse((Get-Date ($timestamp).ToUniversalTime() -UFormat %s)) * $multiplier
return $unixtime
}
<#
Links
https://developers.hubspot.com/docs/methods/contacts/v2/get_contacts_properties
https://developers.hubspot.com/docs/api/crm/contacts
#>
################################################
#
# SCRIPT ROOT
#
################################################
# Load scriptpath
if ($MyInvocation.MyCommand.CommandType -eq "ExternalScript") {
$scriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
} else {
$scriptPath = Split-Path -Parent -Path ([Environment]::GetCommandLineArgs()[0])
}
Set-Location -Path $scriptPath
################################################
#
# SETTINGS
#
################################################
# General settings
$functionsSubfolder = "functions"
$settingsFilename = "settings.json"
################################################
#
# FUNCTIONS
#
################################################
Get-ChildItem -Path ".\$( $functionsSubfolder )" | ForEach {
. $_.FullName
}
$token = "<hapikey>"
<#
$headers = @{
"Authorization" = "Bearer $( $token )"
}
#>
#-----------------
# Demo
#-----------------
$url = "https://api.hubapi.com/integrations/v1/me?hapikey=$( $token )"
$integrations = Invoke-RestMethod -Method Get -Uri $url #-Headers $headers
#-----------------
# Daily Limits
#-----------------
$url = "https://api.hubapi.com/integrations/v1/limit/daily?hapikey=$( $token )"
$limits = Invoke-RestMethod -Method Get -Uri $url #-Headers $headers
# Current usage
$currentApiUsage = $limits.currentUsage
# Current limit
$currentApiLimit = $limits.usageLimit
#-----------------
# CONTACTS - NEW API
#-----------------
$limit = 10
$url = "https://api.hubapi.com/crm/v3/objects/contacts?limit=$( $limit )&archived=false&hapikey=$( $token )"
$contacts = Invoke-RestMethod -Method Get -Uri $url #-Headers $headers
# contacts
$contacts.results
$contacts.results.properties | Out-GridView
# Next url is in
$contacts.paging.next.link
#-----------------
# SEARCH CONTACTS - NEW API
#-----------------
# https://developers.hubspot.com/docs/api/crm/contacts
$url = "https://api.hubapi.com/crm/v3/objects/contacts/search?hapikey=$( $token )"
$body = [ordered]@{
"filterGroups" = @(
@{
"filters" = @(
@{
"propertyName"="lastmodifieddate"
"operator"="GTE"
"value"= ( Get-Unixtime -timestamp ( (Get-Date).AddMonths(-1) ) -inMilliseconds )
}
)
}
)
sorts = @("lastmodifieddate")
#query = ""
properties = @("firstname", "lastname", "email")
limit = 10
after = 0
} | ConvertTo-Json -Depth 8
$search = Invoke-RestMethod -Method Post -Uri $url -ContentType "application/json" -Body $body -Verbose
$search.results
#-----------------
# PROPERTIES
#-----------------
$url = "https://api.hubapi.com/properties/v1/contacts/properties?hapikey=$( $token )"
$properties = Invoke-RestMethod -Method Get -Uri $url -Headers $headers
$properties | Out-GridView
#-----------------
# WORKFLOWS
#-----------------
$url = "https://api.hubapi.com/automation/v3/workflows/?hapikey=$( $token )"
$workflows = Invoke-RestMethod -Method Get -Uri $url -Headers $headers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment