Tiles/WMS sources give you raster data. So, you have to use `QgsRasterLayer()` instead of `QgsVectorLayer()`. Try in this way: ... rlayer = QgsRasterLayer(urlWithParams, 'OpenStreetMap', 'wms') # EDIT THIS LINE if rlayer.isValid(): QgsProject.instance().addMapLayer(rlayer) else: print('invalid layer')