We all started somewhere! Share your experience on the Get Advice: Let's Get Started Sweepstakes thread and be entered to win a $100 credit on your Constant Contact account.

Bad behaviour while adding contacts via API v2

Regular Participant

Bad behaviour while adding contacts via API v2

Hello, people.


We're implementing the v2 API to use with our website. The purpose is to save subscribers in both our database, and in ConstantContact. 


I'm bypassing Oauth by hard-coding the API key and access token in the application. All I need is to retrieve the available contact lists and add e-mails. No fancy double opt-in here since it's part of a sign-up process and they have to intentionally check a checkbox or click a subscribe button. Everything is in PHP.


Here's a code mock-up, what I've used for testing: http://pastebin.com/isTNdgiC


The problems, successively, are as follows:


1. I manually imported 99 e-mails besides mine. That should hit the limit on the free (test) account.

2. I tried adding a contact via script. The script failed with a 403.

3. I removed the trailing slash in the URL (was contacts/?api_key=..., changed to contacts?api_key=) and ran the script again. This time I got a 409 (duplicate).

4. Logged in manually to check the list contents. It did contain the e-mail address that I've added via script.

5. Changed the mail address to "test_".time()."@test.ca" and reran the script several times. Every time it failed with a 403.

6. I receive an e-mail saying that I've hit the limit on the free account. Although the list had 100+ e-mails and did contain the test_234897@test.ca addresses.

7. Proceeded to delete all the contacts in the list.

8. With the list now empty, I ran the script once.

9. Manually checking the list in browser, it contained 1 (one) e-mail address. Script failed with a 403, but the e-mail did get added. Furthermore, while looking at the list, although it said it had 1 contact, it doesn't show up at all. Here's a screenshot: http://i.imgur.com/hwthkir.png

10. Reran the script 3 more times, same behavior with 403 and e-mail addresses not appearing

11. Finally, I've added another e-mail manually, besides the 3 existing, non-appearing ones earlier added via script, and all 4 finally appeared.


I double checked headers and content body to make sure my wanna-be class behaves as in the IO docs. No luck, just 403s. I'm thinking on dropping file_get_contents() in favour of curl_ functions, but I need to understand why it failed.


Can someone point me in the right direction please?


Thank you.





I have spent some time working on your code and I am experiencing the exact same symptoms. BAsed on my troubleshooting, it appears the file_get_contents is opening the stream to the file and providing the JSON payload, which creates the contact. However, somewhere in the process, it appears that your API key is not being preserved.


I know that using PHP cURL will function properly with our API as our PHP wrapper library is built using cURL. If you are interested in making use of that library to jump-start your integration, you can find more information here: http://developer.constantcontact.com/libraries/libraries.html


If you do have any questions, please reply to this post and we will be happy to help!



Elijah G.

Tier 2 Support

Elijah G.
API Support Engineer
Regular Participant

Wow that was fast. Thank you for your reply.


I've seen the libraries earlier today. Frankly it's big, fragmented, and a bit of overkill for what I need, which is just a tiny wrapper class and two bare-minimum methods.


I'm not sure how exactly the API key could get lost in the call though. I didn't find any diagrams describing how the process works (with the HTTP resp. codes being cryptic enough as they are), I'll just try to stuff in more headers tomorrow, maybe I'm missing something.


Thanks again for your reply, that was really quick. :)



I'm a little concerned that you set up your headers in two places, so personally, I would consolidate those to make sure that the correct headers are all always sent to us.  You only need two headers:


Authorization: Bearer xxx

Content-Type: application/json


I'd delete the Action-by (I wasn't aware that could be provided in the header; where did you find that?) and X-Originating-IP headers altogether.  The API defaults to action by owner anyway, so for simplicity, I would omit it for now.


Also, there is a place where you have "lists/?api_key=xxx", where I think you should have "lists?api_key=xxx"; again, it's possible I missed something in the documentation since the V2 API is so new, but as far as I'm aware that trailing slash is uneccessary and would cause issues.


Best Regards,

Shannon W.

API Support Specialist

Regular Participant

Hello, Shannon.

Thank you for your reply.

Slightly offtopic here, but I'm not trying to improve the class. Rather just make it work.
Although I doubt any of your suggestions would be useful, I still implemented them.

1. die(var_dump($ctx)) before stream_context_create() in the add_contact() method. Here's the full array:


2. The Action-By: header was taken from https://constantcontact.mashery.com/io-docs. The Content-Type: header seems to be required, so its pointless to remove it.


3. The trailing slash in the lists retrieval URL has nothing to do with the task at hand, since that method works as expected.

I'm still open to suggestions.
If you wish to test out things, the pastebin link contains ready-to-use code; you just have to fill in an API key and an access token.


Thank you.

The reason the list is empty in our UI is easier to explain.  We employ caching on the UI that could result in the list membership details not updating after an API update to add/modify the list membership.  It could take up to 5 minutes for these updates to show.  We are aware of this issue and have a defect on this, though it involves some complicated caching and it may take awhile for this to be fixed.


The 403 error is a little more difficult to explain.  Will take a look at this further, but the initial direction I'd go in is that two requests are being made by your script, one that has credentials and does the correct behavior (which is why the Contacts are added) and a second that isn't configured correctly and is being returned a 403 correctly for some reason.  All of our PHP examples and all of the PHP we've written use Curl instead of file streams so I don't have a great answer off the top of my head.  Will dig into this and see if we can help figure out what is going on.

Dave Berard
Senior Product Manager, Constant Contact
Regular Participant

Thank you for your reply, Dave.


Just to let you know, in the 403 error, Elijah figured the "for some reason" part yesterday.


string '[{
	"error_message":"The Mashery account associated with this request is not active, or an invalid API key was used."
}]' (length=168)

 I hope this helps.


Developer Portal

View API documentation, code samples, get your API key.

Visit Page