View Single Post
 
Old 01-02-2023, 05:05 PM
Milqn Vuk Milqn Vuk is offline Windows 10 Office 2016
Novice
 
Join Date: Jan 2023
Posts: 14
Milqn Vuk is on a distinguished road
Default Word macro api call

Hello,

I want to connect with an outside API service. I try to make an API cole with the following code below:

Code:
Function GT(prompt As String) As String
  ' I Replace MY_API_KEY with my actual API key
  Dim apiKey As String: apiKey = "API_KEY"
  Dim apiEndpoint As String: apiEndpoint = "https://api.url.com/v1/"
  
  Dim params As String: params = "prompt=" & prompt & "&max_tokens=2048&model=text-davinci-002&stop=.!?"
  
  Dim apiCall As String: apiCall = apiEndpoint & "?" & params
  
  Dim xmlHttp As Object: Set xmlHttp = CreateObject("MSXML2.XMLHTTP")
  
  xmlHttp.Open "POST", apiCall, False
  xmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  xmlHttp.setRequestHeader "Authorization", "Bearer " & apiKey
  
  xmlHttp.send
  
  Dim response As String: response = xmlHttp.rText
  
  Dim responseObject As Object: Set responseObject = JsonConverter.ParseJson(response)
  Dim generatedText As String: generatedText = responseObject("data")(0)("text")
  
  GT = generatedText
End Function
I get the following error: "Compile error" with explanation "Invalid use of new keyword"
Debuging:
Set responseObject = JsonConverter.ParseJson(response)

The error "Set responseObject = JsonConverter.ParseJson(response)" typically indicates that the JsonConverter object or the ParseJson method is not recognized but I think that my API call is not right since I didn't manage to make any successfull API requests. I see this on API endpoint usage to which I have access.

Also, I have added these libraries and references in my project:JSONConverter, Microsoft Scripting Runtime library and Microsoft XML, v6.0 library.

Could anyone give me any advice how to properly call an API?

Would be much appropriated!
Thanks

Last edited by macropod; 01-09-2023 at 04:00 PM. Reason: Added code tags
Reply With Quote