magento 2 update index after adding product in code code example
Example 1: run partial indexing from command line magento 2
bin/magento cronmanager:runjob indexer_update_all_views
Example 2: reindex after post api magento 2
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$indexerCollectionFactory = $objectManager->get("\Magento\Indexer\Model\Indexer\CollectionFactory");
$indexerFactory = $objectManager->get("\Magento\Indexer\Model\IndexerFactory");
$indexerCollection = $indexerCollectionFactory->create();
$allIds = $indexerCollection->getAllIds();
foreach ($allIds as $id) {
$indexer = $indexerFactory->create()->load($id);
$indexer->reindexAll();
}