Create jobs dynamically
This is especially interesting if you have an application that uses our API. An application can have different steps where one can upload files or provide URLs, or even a step where one selects conversion options.
Lets assume an application with the following structure:
- A user's actions leads to the creation of a job
- User is able to define different conversions
- User is able to specify which inputs are wanted
In this case, a job can be created step by step, defining everything needed in each step.
For 1., create the job and save the id
or token
.
A job can be created providing the field process
with the value false
, so the job will not start processing even though it has the required data.
POST /jobs HTTP/1.1
Host: api2.online-convert.com
x-oc-api-key: <your API key here>
Content-Type: application/json
Cache-Control: no-cache
{
"process": false
}
curl -X POST \
https://api2.online-convert.com/jobs \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-H 'x-oc-api-key: <your API key here>' \
-d '{
"process": false
}'
- For 2., take the conversion(s) defined by the user and feed them to our API using the POST
/jobs/{id}/conversions
endpoint.
POST /jobs/8c267416-25dc-4298-8a26-b48f7eaa8000/conversions HTTP/1.1
Host: api2.online-convert.com
x-oc-api-key: <your API key here>
Content-Type: application/json
Cache-Control: no-cache
{
"target": "png"
}
curl -X POST \
https://api2.online-convert.com/jobs/8c267416-25dc-4298-8a26-b48f7eaa8000/conversions \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-H 'x-oc-api-key: <your API key here>' \
-d '{
"target": "png"
}'
- For 3., take the input(s) defined by the user and feed them to our API using the POST
/jobs/{id}/input
endpoint.
POST /jobs/8c267416-25dc-4298-8a26-b48f7eaa8000/input HTTP/1.1
Host: api2.online-convert.com
x-oc-api-key: <your API key here>
Content-Type: application/json
Cache-Control: no-cache
{
"type": "remote",
"source": "https://oc.ocstatic.com/example-file/raster%20image/jpg/example_small.jpg"
}
curl -X POST \
https://api2.online-convert.com/jobs/8c267416-25dc-4298-8a26-b48f7eaa8000/input \
-H 'cache-control: no-cache' \a
-H 'content-type: application/json' \
-H 'x-oc-api-key: <your API key here>' \
-d '{
"type": "remote",
"source": "https://oc.ocstatic.com/example-file/raster%20image/jpg/example_small.jpg"
}'
If you have more than one input to send in the same POST request, you can define them inside an array as in the following example:
POST /jobs/8c267416-25dc-4298-8a26-b48f7eaa8000/input HTTP/1.1
Host: api2.online-convert.com
x-oc-api-key: <your API key here>
Content-Type: application/json
Cache-Control: no-cache
[{
"type": "remote",
"source": "https://oc.ocstatic.com/example-file/raster%20image/jpg/example_small.jpg"
},{
"type": "remote",
"source": "https://oc.ocstatic.com/example-file/raster%20image/png/example_small.png"
}]
curl -X POST \
https://api2.online-convert.com/jobs/8c267416-25dc-4298-8a26-b48f7eaa8000/input \
-H 'cache-control: no-cache' \a
-H 'content-type: application/json' \
-H 'x-oc-api-key: <your API key here>' \
-d '[{
"type": "remote",
"source": "https://oc.ocstatic.com/example-file/raster%20image/jpg/example_small.jpg"
},{
"type": "remote",
"source": "https://oc.ocstatic.com/example-file/raster%20image/png/example_small.png"
}]'
- After these steps, the job is ready to be processed. Thus, issue a
PATCH
request to the/jobs/{id}
endpoint by sending theprocess
field with the valuetrue
. - Alternatively, a
callback
parameter with the URL to a previously set up callback script that will handle the information of the completed job could also be set here.
PATCH /jobs/8c267416-25dc-4298-8a26-b48f7eaa8000 HTTP/1.1
Host: api2.online-convert.com
x-oc-api-key: <your API key here>
Content-Type: application/json
Cache-Control: no-cache
{
"process": true
}
curl -X PATCH \
https://api2.online-convert.com/jobs/8c267416-25dc-4298-8a26-b48f7eaa8000 \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-H 'x-oc-api-key: <your API key here>' \
-d '{
"process": true
}'
- After this, if no callback URL has been passed, it's possible to check the status of the job via polling.