Skip to main content
New Participant
Jul 31, 2023 8:00 AM UTC

Files listing api call

Hi Support,
I was looking for an API call to list all items in a folder including files inside the children and sub-children folders.
I checked the https://developer.box.com/reference/get-folders-id-items/ API call, but it lists only files and folders inside that exact folder rather than child folders.
I am looking forward to the community response.

    3 replies

    CodeBoxSeb
    Box Employee
    Jul 31, 2023 9:39 AM UTC

    Hi @user49 ! Welcome to the Forum ! 😃

    Very good question ! I don’t see any solution to retrieve all the files from a path.

    You can try the SEARCH endpoint : https://developer.box.com/reference/get-search/
    This would allow you to list files for a folder and a subfolder, but I think you would need to specify a search term in your query.

    For your parameters, you can select :
    -type : this will allow you to limit the search to file types
    -ancestor_folder_ids : this will allow you to return results for a specific file path.

    Translate
    user49Author
    New Participant
    Jul 31, 2023 10:23 AM UTC

    Thanks for the reply @CodeBoxSeb
    I tried this API but “query” parameters seem to be a mandatory field, although is marked optional in the documentation. I get the error message “‘to_search’ is required” with 400 bad request error. Is there any way I can execute a wildcard search on the query parameter?

    Translate
    rbarbosa Box
    Developer Advocate
    Aug 1, 2023 3:14 PM UTC

    Hi,

    You can recursively list all items…

    Here is a python example:

    from typing import List
    from boxsdk import JWTAuth, Client
    from boxsdk.object.item import Item
    from boxsdk.object.folder import Folder
    
    
    class SimpleItem:
        """simple item class"""
    
        box_type: str
        id: str
        name: str
        parent_id: str
    
        def __init__(self, item: Item, parent_id: str = None):
            self.box_type = item.type
            self.id = item.id
            self.name = item.name
            self.parent_id = parent_id
    
        def __repr__(self):
            return f"{self.box_type}\t{self.id}\t{self.name}\t{self.parent_id}\n"
    
    
    class CFG:
        """config class"""
    
        JWT_CONFIG_FILE = ".jwt.config.json"
        AS_USER = "18622116055"
        PARENT_FOLDER_ID = "172599089223"  # folder id 0 is root folder
    
    
    def get_box_client(as_user: bool = False):
        """get a box client"""
        auth = JWTAuth.from_settings_file(CFG.JWT_CONFIG_FILE)
        service_client = Client(auth)
        if not as_user:
            return service_client
        user = service_client.user(CFG.AS_USER)
        return service_client.as_user(user)
    
    
    def folder_items(folder: Folder) -> List[Item]:
        """list folder items recursively"""
        items = folder.get_items()
        result = []
        for item in items:
            simple_item = SimpleItem(item, folder.id)
            result.append(simple_item)
            if item.type == "folder":
                result.extend(folder_items(item))
        return result
    
    
    def main():
        """main function"""
    
        client = get_box_client(as_user=True)
    
        # get starting folder
        folder = client.folder(CFG.PARENT_FOLDER_ID).get()
    
        folder_list = folder_items(folder)
    
        print(folder_list)
    
    
    if __name__ == "__main__":
        main()
        print("\n")
        print("-" * 80)
        print("All Done!")
    
    

    Resulting in:

    ❯ python main.py
    [folder	176840203842	Cenotes	172599089223
    , folder	176841790581	2022-10-16	176840203842
    , folder	176838913195	Jane Smith	176841790581
    , file	1037492412345	Box-Dive-Waiver.pdf	176838913195
    , folder	178059063249	2022-10-21	176840203842
    , folder	178059476189	Barbosa	178059063249
    , file	1044375500347	Box-Dive-Waiver.pdf	178059476189
    , file	1044379452138	dan-sample.jpeg	178059476189
    , file	1044391737893	padi-sample.jpeg	178059476189
    , folder	176840211427	Eagle Ray Bay	172599089223
    , folder	176840892622	2022-10-16	176840211427
    , folder	176840808257	Jane Smith	176840892622
    , file	1037494109933	Box-Dive-Waiver.pdf	176840808257
    , folder	176841144813	Ras Mohamed	172599089223
    , folder	176842575003	2022-10-16	176841144813
    , folder	176840991040	Jane Smith	176842575003
    , file	1037491776095	Box-Dive-Waiver.pdf	176840991040
    , folder	176839738500	Sharks Bay	172599089223
    , folder	176841319327	2022-10-16	176839738500
    , folder	176841099298	Jane Smith	176841319327
    , file	1037492217945	Box-Dive-Waiver.pdf	176841099298
    , folder	176838773123	Thistlegorm	172599089223
    , folder	176834671641	2022-10-16	176838773123
    , folder	176840496781	Jane Smith	176834671641
    , file	1037498578259	Box-Dive-Waiver.pdf	176840496781
    , folder	191152978144	2023-01-21	176838773123
    , folder	191155574713	John Smith	191152978144
    , file	1118910257949	Box-Dive-Waiver.pdf	191155574713
    , folder	175969946454	Tropicana	172599089223
    , folder	175970887672	2022-10-05	175969946454
    , folder	175971722615	Barbas	175970887672
    , file	1032450614414	Box-Dive-Waiver.pdf	175971722615
    , file	1032446518305	CARD-CERTIFICATION.jpg	175971722615
    , file	1032444093760	CARD-INSURANCE.jpg	175971722615
    , folder	176841518705	2022-10-16	175969946454
    , folder	176841372656	Jane Smith	176841518705
    , file	1037493295732	Box-Dive-Waiver.pdf	176841372656
    ]
    

    Let us know if this helps.

    Cheers

    Translate