How to generate and deploy bpmn with fluent api?

I know how to use the fluent API to generate BPMN.
But have no idea how to deploy the generated BPMN at runtime.

I tried the below code to deploy the BPMN but it does not work.

repositoryService.createDeployment().addModelInstance(key + ".bpmn",instance).deploy();

and

Deployment deployment = repositoryService.createDeployment().
                    enableDuplicateFiltering(true).
                    tenantId(null)
                    .addInputStream("12312313213123.bpmn",
                            new ByteArrayInputStream(
                                    ((GeneratedProcessDefinition) definition).getBpmn().getBytes())
                    ).deploy();

Above code not throw any exception but after that executed when I try to get the process definition by the key the process engine says there is no such process definition exists.

How can I deploy a BPMN at runtime?
Thanks.

@himly, Refer this docs page for fluent-builder-api and bpmn-model-api and create-a-bpmn-model.

Refer the below example on how to use fluent api to create a model and deploy it.

// validate and write model to file
Bpmn.validateModel(modelInstance);
File file = File.createTempFile("bpmn-model-api-", ".bpmn");
Bpmn.writeModelToFile(file, modelInstance);

To deploy the bpmn file:

@Autowired
private RepositoryService repositoryService;

public DeploymentWithDefinitions deployProcess(){
	
   DeploymentWithDefinitions deploymentWithDefinitions = repositoryService.createDeployment().name("myProcess")
			.tenantId("tenantId").enableDuplicateFiltering(true).source("clientrepo")
			.addInputStream("bpmnFile", new ByteArrayInputStream(bpmnModelByteArray)).deployWithResult();

	Assert.notEmpty(deploymentWithDefinitions.getDeployedProcessDefinitions(), "Process should not be empty");
	
	return deploymentWithDefinitions;
}