You can build a market scanner via the TWS API by defining a Scanner Subscription object, and then making a request using the EClient.reqScannerSubscription() function. Please see the documentation of this functionality here.
If you are facing issues building a market scanner via the API, please ensure that you can create a similar scanner manually via the Advanced Market Scanner in the TWS.