Issues figuring out syntax for tasks.add_tag()

Hi all! I am exploring the Asana API and trying to make my first script. I am having issues figuring out the correct syntax to use for the tasks.add_tag() function. Below is a snippet of one of the many configurations I have tried:

sample_tag = client.tags.get_tag([sample gid goes here])


I have confirmed that the sample_tag is returning the tag information.

Here is the error this code gives me on the console:

asana.error.InvalidRequestError: Invalid Request: tag: Missing input

I have tried reviewing the documentation and am still unclear on how to send this request properly. I’d appreciate your support!

Also, I am using the Python client library!

Did you check out the source of the library? It usually helps a lot.

Based on the API doc (Add a tag to a task) I would that that you have the create the tag first, get a gid, and then use this id to add the tag to the task. It is a 2-step process.

Thank you for your response! I did check the source and it could be that the APIs I have used before felt different, but I am still not clear on what the issue with my syntax is.

The task I referenced when running my code already exists and its GID is correct (I was able to pull the tag information when getting the tag via its GID). This tag was created in Asana before running the code.

Is it not possible to use an existing tag, or are you saying that I need to “create it” locally somehow before adding it to a task? When passing the full dict object into the add_tag function it also did not work.

Your code is attaching sample_tag to task gid 1202815185779984 when in fact you should be attaching tag 1234563753663 to task 1202815185779984. Does it make sense?

After the initial creation of the tag, you can’t use its text value anymore, you have to work with its gid.

1 Like

I apologize! This is one of the things I tried and did not work but not the sample code I put in and I did not remember that. I’m not sure whether the GID needs to go as a params= or data=. I’ll try both in a bit and paste my console results.

So even when passing a tag ID it does not work? I suggest you download their Postman library and run requests from there to make sure the syntax is correct.

1 Like

It still does not work with a proper id! Here is what the console gives me:

ValueError: dictionary update sequence element #0 has length 1; 2 is required

I will look into downloading their Postman library. I unfortunately have not been able to run Postman in my device before but this is probably worth a couple of hours of troubleshooting.

Can you post your latest code please? which language is it?

It’s in Python! Here’s the code:

sample_task = client.tasks.get_task('1203557436241232')
print("Here is the task's name:")

sample_tag = client.tags.get_tag('1202490368795511')
print("Here is the tag's color:")

##Code uses the same task and tag

And the output:

Here is the task's name:
Sample Task
Here is the tag's color:
Traceback (most recent call last):
  File "C:\Users\thele\Documents\Development\Asana\Workflow-Automation\", line 29, in <module>
  File "C:\Users\thele\AppData\Local\Programs\Python\Python39\lib\site-packages\asana\resources\", line 385, in add_tag
    return, params, **options)
  File "C:\Users\thele\AppData\Local\Programs\Python\Python39\lib\site-packages\asana\", line 197, in post   
    'data': _merge(parameter_options, data),
  File "C:\Users\thele\AppData\Local\Programs\Python\Python39\lib\site-packages\asana\", line 367, in _merge 
    [result.update(obj) for obj in objects]
  File "C:\Users\thele\AppData\Local\Programs\Python\Python39\lib\site-packages\asana\", line 367, in <listcomp>
    [result.update(obj) for obj in objects]
ValueError: dictionary update sequence element #0 has length 1; 2 is required

If you look at the API doc Add a tag to a task and the python source code python-asana/ at master · Asana/python-asana · GitHub I am wondering if you shouldn’t give an object {tag: '123344'} as a parameter… :thinking:

IT WORKED! Thank you so much!!

Here is the successful code in case anyone looks at this thread in the future:

client.tasks.add_tag('1203557436241232',{'tag': '1202490368795511'})

Hello. I have the same problem but I use java api library.
When I execute teh following code:
client.tasks.addTag("1204525991564977").data("gid", "1204520237142577").execute();
I get the following error: 400 Bad Request
{"errors":[{"message":"task: Not the correct type","help":"For more information on API status codes and how to handle them, read the docs on errors:"},{"message":"tag: Missing input","help":"For more information on API status codes and how to handle them, read the docs on errors:"}]}

What do I do wrong?
P.S. the tag gid is correct, the tag has been created manually in the UI and it can be added to the task by hand, but I failed to do this via API. Any help is much appriciated.

Can you confirm the first id (1204525991564977) is the task gid and then the other one is the tag? That’s what’s it is supposed to be according to the code specs. Also the tag id should be given as a value for a key called “tag”.

CleanShot 2023-05-04 at 09.14.11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.