Adding Tags Input Value Error (REST)

EDIT: This was a pretty simple error on my side, answer is at the bottom.

Hi,

I have been trying to add tags (multiple) via the REST api. I can do it here:

https://developer.infusionsoft.com/docs/rest/#!/Tags/applyTagToContactIdsUsingPOST

And I can do a single tag without an issue in my Node app, but it errors out when I try to do multiple tags.

I have searched on here and I thought this post (Apply tags to contact Error - REST) should have solved it for me, but I still am getting an error.

Error:
ADD TAG: {“method”:“POST”,“url”:“https://api.infusionsoft.com/crm/rest/v1/contacts/196033/tags",“headers”:{“Authorization”:"Bearer -----------------------”,“Content-Type”:“application/json”},"body":{“tagIds”:[“3179,3303”]},“json”:true}
TAG BODY: (184): [object Object] – Parsing – {“message”:“Input value is invalid: 3179,3303”}

My code looks like this:

function keapCreateContact(keapPhone, keapURL, keapMarketInfo, keapTags, accessToken) {
    return new Promise((resolve, reject) => {
        const rootUrl = 'https://api.infusionsoft.com/crm/rest/v1';
        const authToken = 'Bearer ' + accessToken;

        const createContact = {
            method: 'PUT',
            url: rootUrl + '/contacts',
            headers:
            {
                Authorization: authToken,
                'Content-Type': 'application/json'
            },
            body: {
                phone_numbers: [{ field: 'PHONE1', number: keapPhone }],
                custom_fields: [{ content: keapURL, id: 183 }, { content: keapMarketInfo, id: 177 }],
                duplicate_option: 'Email',
            },
            json: true
        };

        request(createContact, function (error, response, body) {
            if (error) {
                reject('createContactErr');
            } else {
                if (body.toString().substring(0, 4) === "<h1>") {
                    reject('invalidToken');
                } else {
                    const contactId = body.id;
                    const addTag = {
                        method: 'POST',
                        url: rootUrl + '/contacts/' + contactId + '/tags',
                        headers:
                        {
                            Authorization: authToken,
                            'Content-Type': 'application/json'
                        },
                        body: { tagIds: [keapTags] },
                        // body: { tagIds: [2063, 1085] },
                        json: true
                    };
                    console.log("ln 239: ");
                    console.log(body);
                    console.log("ADD TAG: " + JSON.stringify(addTag));

                    request(addTag, function (error, response, body) {
                        if (error) {
                            reject('addTagErr');
                        } else {
                            resolve('addTagSuccess')
                        }
                    });
                }
            }
        });
    });
}

I am stumped. Any ideas would be great.

Thanks

I’ve not seen an instance of this approach working due to CORS violations. I have to send my js call to a php script on the source server and make the api call from there, then return a response to the JS caller. You can’t call a foreign server from JS without violating CORS (it’s not open/enabled on the api server and really shouldn’t be).

Thanks for taking the time to reply, John.

This part may just be going over my head:

can’t call a foreign server from JS without violating CORS

I’m not sure what “foreign server” means here, this is all being done from one server calling the REST API. Is that a setup that would work to add a single tag but not multiple?

Thanks

your webpage is generated by a server…that’s the source server. The JS when run in the browser tab will only accept communications with the source server. Trying to use JS to call a foreign server (the api server) will not work because if violates Cross Origin Resource Sharing…basically, it’s there to protect people from getting a virus/hacked. JS is called from your computer (not a server)… so to get your server to make the call, JS must talk to it and then get the call to come from the source server otherwise it won’t be allowed. You can’t call it from the browser (which is what JS would have to do if it tried to do so directly).

But in that case shouldn’t the CORS issue prevent me from adding the contact to InfusionSoft/Keap in the first place? And not just becoming problematic when trying to add 2 tags as opposed to 1?

Edit: To maybe clarify, this is all happening on one server. Basically Twilio is getting a text, and then adding the phone number to InfusionSoft all in one sequence. Nothing is happening on a webpage/browser.

Not sure I understand then… if twilio is doing anything then it’s via the api and through the method mentioned…so if you’re, outside of twilio, trying to apply tags then how are you actually doing it?

If you’re doing it and you want multiple tags, the url has to be /contacts/{contactID}/tags

image

So, I think I’ve narrowed down the issue.

I’ve actually gotten it to work now when I hard-code it, but when I’m inserting tags into my code dynamically it fails.

If I JSON.stringify it when I do it dynamically it looks like:

{"tagIds": ["3033, 2103"]}

and when I hard code it and JSON.stringify it I get:

{"tagIds": [3033, 2103]}

So…I’ll work on figuring that out.

Thanks

EDIT:

For anyone wondering, I was putting my tags into an array like this:

... keapTags: "2345, 2343"

and then inserting it like:

... tagIds: [ keapTags ],

I fixed it by putting my tags into an array like this:

... keapTags: [ 2345, 2343 ],

and then inserting them like this:

... tagIds: keapTags,