@@ -17,14 +17,55 @@ public HubspotWorkflow()
17
17
Group = "CRM" ;
18
18
}
19
19
20
+ [ Setting ( "Hubspot API Key" , Description = "Enter the API Key from your HubSpot account" , View = "TextField" ) ]
21
+ public string HubspotApiKey { get ; set ; }
22
+
23
+ private Uri HubspotApiUrl
24
+ {
25
+ get
26
+ {
27
+ return new Uri ( $ "https://api.hubapi.com/crm/v3/objects/contacts?hapikey={ HubspotApiKey } ") ;
28
+ }
29
+ }
30
+
31
+ static readonly HttpClient client = new HttpClient ( ) ;
20
32
public override WorkflowExecutionStatus Execute ( Record record , RecordEventArgs e )
21
33
{
22
34
return WorkflowExecutionStatus . Completed ;
23
35
}
24
36
25
37
public override List < Exception > ValidateSettings ( )
26
38
{
27
- return new List < Exception > ( ) ;
39
+ var errors = new List < Exception > ( ) ;
40
+
41
+ // Verify API key is not empty
42
+ if ( string . IsNullOrWhiteSpace ( HubspotApiKey ) )
43
+ {
44
+ errors . Add ( new Exception ( "Hubspot API key is missing" ) ) ;
45
+ return errors ;
46
+ }
47
+
48
+ // Make a super simple GET request to fetch contacts in HubSpot
49
+ // This way with we can verify that the API key is valid
50
+ // https://developers.hubspot.com/docs/api/crm/contacts
51
+ var testResponse = client . GetAsync ( HubspotApiUrl ) . Result ;
52
+
53
+ if ( testResponse . IsSuccessStatusCode == false )
54
+ {
55
+ // Invalid key will return a 401
56
+ // Message property of response contains useful message
57
+ // The API key provided is invalid. View or manage your API key here: https://app.hubspot.com/l/api-key/
58
+ var errorResponse = testResponse . Content . ReadAsStringAsync ( ) . Result ;
59
+ var errorObj = JsonConvert . DeserializeObject < ErrorResponse > ( errorResponse ) ;
60
+ errors . Add ( new Exception ( errorObj . message ) ) ;
61
+ }
62
+
63
+ return errors ;
28
64
}
29
65
}
66
+
67
+ public class ErrorResponse
68
+ {
69
+ public string message { get ; set ; }
70
+ }
30
71
}
0 commit comments