Rest API curl search command - How to search for card on a board

I’ve figured out how to search for a card, but I can’t figure out how to search on a particular board.

this works fine:
https://api.trello.com/1/search?modelTypes=cards&query=name:[‘title123’]&key=[mykey]&token=[mytoken]

I’ve tried everything to add the boardId to the search w/o luck. Help appreciated.
Documentation:
The Trello REST API (atlassian.com)

@GlennWilliams

Review the How To Search In Trello page or the Search for cards page, where declaring the board in a search query is described.

I think the REST API documentation is currently being changed, as the parameter for declaring the board used to be the query parameter ‘board’, not a request parameter ‘idBoards’. The former is what is described on those two other pages and what still works on Trello itself.

Here is an example where query parameter ‘board’ is used to find all cards on a particular board that are open:

https://api.trello.com/1/search?query=board:602786e91c69d30e45a9ccd8 is:open&key={ApiKey}&token={ApiToken}

And in the response, the two parameters are properly parsed as search modifiers:

"modifiers": [
            {
                "text": "board:602786e91c69d30e45a9ccd8"
            },
            {
                "text": "is:open"
            }
],

Searching by board name also works:

https://api.trello.com/1/search?query=board:"my board" is:open&key={ApiKey}&token={ApiToken}

The REST API documentation says the request parameter idBoards is meant to be a thing called a ‘oneOf’:

idBoards
oneOf [string, string]
mine or a comma-separated list of Board IDs
Style: form

…not a comma separated string, as per every single other array parameter on the page!

I’m not sure why there are two separate ways to define the board(s) to be included in the search, or what happens if the two were to overlap or contradict each other

This is the second time in the past few days (refer to this question) that I’ve noticed a previous text based parameter in the Trello REST API docs has disappeared and replaced by a new ID based parameter. There have been no announcements from the Trello team about these changes.

1 Like

Thanks so much for the details. Very helpful and I’m getting closer. With your help I can search by board + name:
https://api.trello.com/1/search?query=board:5c...8a3 name:[‘testCardDontDelete’] &key=dfc9…

Unfortunately the name has to be an exact match. Grrrr. Is this what you would expect?

I cannot replicate that issue.

  1. If I search for cards with name:"cat", cards with that word in their name are returned.
  2. If I search for cards with name:"dead cat", cards with that phrase in their name are returned.

I do not have to do an ‘exact match’ for the whole name of the card.

Also, if I have provided the correct answer to your original question, please mark it as such.

So, clearly I’m doing something that the api doesn’t like. Would you be willing to take a look at what I’m doing? Again, thanks for your time.

    static public string test()
    {
        var url = "https://api.trello.com/1/search?query=board:5c92...48a3 is:open name:['testCard'] &key=dfc....598&token=ec7b...10";
        var httpRequest = (HttpWebRequest)WebRequest.Create(url);
        var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
            return result;
        }
    }

Hi Glenn,

I may have misunderstood the question, but have you tried adding the idBoards query parameter instead of trying to add it to the query?

https://{{trello_api_url}}/search?query=water&idBoards={{trello_board_id}}&modelTypes=cards&card_fields=name,desc&key={{trello_api_key}}&token={{trello_api_token}}

Here is the core of my C# code…

public class TrelloService {

	public TrelloService(IHttpClientFactory clientFactory)
	{
		_clientFactory = clientFactory;
	}

	public async Task<List<TrelloCard>> SearchCardsOnBoardAsync(string boardId, string query)
	{
		var url = BuildUrl("search", new Dictionary<string, string>() {
			{ "idBoards", boardId },
			{ "modelTypes", "cards" },
			{ "card_fields", "name,desc" },
			{ "query", query }
		});
		var result = await PerformGetAsync<TrelloSearchResult>(url);

		return result?.Cards;
	}

	protected async Task<T> PerformGetAsync<T>(string url)
	{
		var request = new HttpRequestMessage(HttpMethod.Get, url);
		var client = _clientFactory.CreateClient(HTTP_NAME);
		var response = await client.SendAsync(request);

		return await ProcessResponse<T>(response);
	}

	protected async Task<T> ProcessResponse<T>(HttpResponseMessage response)
	{
		if (response.IsSuccessStatusCode)
		{
			try
			{
				var json = await response.Content.ReadAsStringAsync();
				if (!string.IsNullOrEmpty(json))
				{

					return JsonSerializer.Deserialize<T>(json, SerializerOptions());
				}
			}
			catch (Exception ex)
			{
				throw new TrelloServiceException("Unable to deserialize response", ex);
			}
		}
		else if (response.StatusCode != HttpStatusCode.NotFound)
		{
			await ThrowTrelloServiceError(response);
		}

		//else
		return default(T);
	}
}

TrelloSearchResult

public class TrelloSearchResult
{
	public TrelloSearchOptions Options { get; set; }

	public List<TrelloCard> Cards { get; set; }
	public List<TrelloAction> Actions { get; set; }
	public List<TrelloBoard> Boards { get; set; }
	public List<TrelloOrganization> Organizations { get; set; }
	public List<TrelloMember> Members { get; set; }


	public class TrelloSearchOptions
	{
		public List<string> ModelTypes { get; set; }
		public List<Dictionary<string, string>> Terms { get; set; }
		public bool Partial { get; set; }
	}
}

BTW - I used List<> where IEnumerable<> would have been better as I don’t use any of the IList<> features.

Thanks so much Rob. The code is very helpful and I think I now understand what a ‘query’ is. You would think Trello would give some example calls, but no.

I’ll run this by you. I’d like to see a way to query only closed=false cards, but I don’t see where to add this to the query. Am I missing something?

Rob, I just stumbled across this. This url contiains a boardID parameter, but it’s ignored. The search actually will return cards not on the board in the url. 8( Crazy.

https://{{trello_api_url}}/search?query=water&idBoards={{trello_board_id}}&modelTypes=cards&card_fields=name,desc&key={{trello_api_key}}&token={{trello_api_token}}

Hi Glenn…

First off, I discovered a mistake (omission) from my original url - namely, you should include the &partial=true param if you want partial word matches (e.g. search for “trell” will return “Trello” and “trellis”). Without the partial=true, the search looks for full word matches.

So here is my new url:

/search?key={{trello_api_key}}&token={{trello_api_token}}&query=sum&idBoards={{trello_board_id}}&modelTypes=cards&card_fields=name,desc,closed&partial=true

From your last comment… are you saying that you have tried this url setup but are getting results from boards other than the one indicated by the {{trello_board_id}} var in my request? I have tested this on a couple of workspaces and multiple boards. I am seeing results restricted to the board id that I put in.

I’m positive. I accidently left my test board hardcoded which is how I discovered. I’m positive. I put several valid boadIds and the same results were returned. It does throw an error if you put in an invalid boardId. Thanks.

Crazy! I have just switched up the board ids in my code and each return results specific to the board. I did verify that an invalid board id will result in an error.

I wish I could help. If I come up with anything, I will let you know.

ROB

All I can say is I’m an idiot. I don’t know what I did the other day, I was so sure. Working fine for me today. Ugg. Thank you Mr. Banning.

Been off the discussion board, but glad to hear that it is working now.

Cheers
ROB