I need to add a tag to a contact using API.
I’m using Active Campaign resources according to API V3 documentation (https://developers.activecampaign.com/reference#create-contact-tag), making a POST request. I've got other requests working like adding and getting contacts and adding and getting accounts the same way.
However, making the request i just receive a 400 Bad request status, including the error No tag id provided. I have checked both tag id and contact id exist.
I'm using this function to fill the contact tag, I checked and both are filled when sending.
private void UpsertTag(GetAllTagsOutput output, string contactId, string tagvalue)
{
TagMeta tag = output.Tags.FirstOrDefault(c => c.Tag == tagvalue);
ContactTag contactTag = new ContactTag
{
Contact = contactId,
Tag = tag.Id
};
this.service.Upsert(contactTag);
}
The contacttag class looks like this:
[Api("contactTags")]
public class ContactTag : ActiveCampaignRef
{
/// <summary>
/// Contact ID
/// </summary>
public string Contact { get; set; }
/// <summary>
/// Tag ID
/// </summary>
public string Tag { get; set; }
}
Then i upsert it using
public T Upsert<T>(T item, IDictionary<string, string> parameters = null) where T : ActiveCampaignRef
{
if (IsNestedObject(item))
{
return requestService.Post<T>(GetPath(item), item);
}
var responseValues = requestService.Post<Dictionary<string, object>>(GetPath(item), item, parameters);
return JsonConvert.DeserializeObject<T>(responseValues[GetItemName(item)].ToString());
}
The get path does return the expected path https://youraccountname.api-us1.com/api/3/contactTags
finally i got a post
public TResponse Post<TResponse>(string path, object data, IDictionary<string, string> parameters = null)
{
return this.Send<TResponse>(path, "POST", data, parameters);
}
private TResponse Send<TResponse>(string path, string method, object data = null, IDictionary<string, string> parameters = null)
{
return JsonConvert.DeserializeObject<TResponse>(this.SendRequest(path, method, data, parameters));
}
private string SendRequest(string path, string method, object data = null, IDictionary<string, string> parameters = null)
{
var finalPath = path;
if (parameters != null && parameters.Any())
{
// Add parameters to the request
var queryString = string.Empty;
foreach (var param in parameters)
{
queryString = queryString + "&" + param.Key + "=" + param.Value;
}
finalPath += "?" + queryString;
}
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + finalPath);
request.Method = method;
request.Headers.Add("API-Token", apiKey);
AttachDataIfNecessary(request, data);
try
{
var webResponse = (HttpWebResponse)request.GetResponse();
using (Stream dataStream = webResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(dataStream);
string response = reader.ReadToEnd();
return response;
}
}
catch (WebException e)
{
throw GetActiveCampaignException(e);
}
}
Method: POST Status: BadRequest Response: {"errors":[{"title":"Invalid value for query parameter contactTag.tag","detail":"No tag id provided","source":{"parameter":"contactTag.tag"}}]}'
So I was running into this issue (with php), and it took me quite a while to figure this out, but for me the problem is that there is a small bug in the documentation of the API, and at first I posted
'body' => '{"contact":"1","tag":"2"}',when the needed body is:
'body' => '{"contactTag":{"contact":"1","tag":"20"}}',I am not sure that this is exactly your error since you are creating this contactTag object, but I did get exactly the same error, and for me using the second body worked.