active_campaign icon indicating copy to clipboard operation
active_campaign copied to clipboard

Can't get API to work at all

Open lorenzsell opened this issue 3 years ago • 5 comments

I'm having all sorts of issues with the API and can't tell if it's just me or if the documentation is out of date.

First, I'm unable to connect with:

client = ::ActiveCampaign::Client.new(
        api_url: 'YOUR-ENDPOINT', # e.g. 'https://youraccount.api-us1.com/api/3'
        api_token: 'YOUR-API-KEY')

I have to use api_endpoint and api_key instead of api_url and api_token and it doesn't work at all with the /api/3 endpoint. I have to point it at /admin/api.php. This isn't documented anywhere. I had to parse it together from support requests and a lot of hacking.

Once I'm actually connected to the API, I can hardly make any calls. For example, I've tried every possible variation I can think of to retrieve all lists: client.lists, client.get_lists, client.list.

They all generate a version of:

client.show_lists => 
NoMethodError: undefined method `show_lists' for #<ActiveCampaign::Client:0x00007fbaede2b6c0>

The only thing that generates a response is client.list_list which gives me the following:

=> {"result_code"=>0,
 "result_message"=>"Failed: Nothing is returned",
 "result_output"=>"json",
 "results"=>[]}

lorenzsell avatar Aug 22 '21 15:08 lorenzsell

same!

jmburges avatar Sep 15 '21 11:09 jmburges

same

shenst1 avatar Sep 15 '21 18:09 shenst1

@lorenzsell @jmburges @shenst1 -- I think the problem here is that master branch is not consistent with what is pushed to Rubygems.

see last known published gem https://github.com/mhenrixon/active_campaign/tree/v0.1.16

sync_contact became contact_sync add_contact became contact_add etc

jasonfb avatar Nov 02 '21 14:11 jasonfb

this seems like all developer confusion to me. I went down these same rabbit holes, explained here: https://github.com/mhenrixon/active_campaign/pull/84

@lorenzsell @jmburges @shenst1

as well, you must take note that the comment in the setup has the critical piece of hidden information: /admin/api.php must be added to your active campaign subdomain for it to work (this is all working on the V1 API)

client = ::ActiveCampaign::Client.new( api_endpoint: 'https://[YOUR ACTIVE CAMPAIGN DOMAIN]/admin/api.php', # e.g. 'https://yourendpoint.api-us1.com/admin/api.php' api_key: '[YOUR-API-KEY]')

@mhenrixon -- as a point of feedback, your code has lots of comments with important hidden details, which other developers (like me and the ones above) won't read when we're moving quickly. I'd recommend making some of this stuff more explicit in the actual code so that our editors show it as a color we should pay attention to (and not grey, signaling irrelevance.) just to make it easier to grok for people new to the gem.

jasonfb avatar Nov 02 '21 14:11 jasonfb

also to debug the actual API, I had to do a little low-level monkey patching. Since the published version of the gem is currently old and I see that master is refactored, I won't bother making a pull request. As well, I see that on the new gem code there are already begin/rescue to handle this.

however, when working on v0.1.16, you will get crashes on client.rb line 71 if the JSON fails to parse.

To debug it, I had to re-write the method like to see the actual responses from the API:

I would highly recommend patterns like this when making wrappers because you can't anticipate all of the things that can (will) go wrong with these responses and it's probably best just to leave the responses spit back to us the developer to figure out where went wrong.

 def request(method, api_method, data)
      req = create_request method, api_method, data
      response = HTTPI.send(method, req)
      begin
        response = JSON.parse(response.body)
      rescue StandardError => e
        puts "*********  There was an error parsing the response from ActiveCampaign: #{response.inspect}"
        raise(e)
      end

      normalize(response)
    end

jasonfb avatar Nov 02 '21 14:11 jasonfb