We don't have an API route out of the box to perform allocations in this manner.
If the sales order or credit note had payments attached when processed, that would have automatically created a cash book entry and when that was processed it would have allocated against the sales order or credit note.
To perform discrete allocations where you can specify an invoice and which credits to allocate to, or vice-versa, then a custom plugin adding the route would need to be made.
Not a difficult or time consuming thing to do for a developer with experience of Jiwa and the API - but not something I personally am prepared to do for you for free via forum posts.
You can ask for us to quote to do it for you via our
helpdesk.