Handwritten document recognition API

Updated at:

1. Overview

The processDocument operation recognizes text and content blocks from a single handwritten document image. For this operation, the engineType parameter must be set to RecognizeWritten.

Item

Description

Request method

POST

Service address

https://cloud-iqs.aliyuncs.com/document/process

Request format

application/json

Authentication method

The X-API-Key request header

Note
  • Billing for this operation is based on Vertical Search. You are charged for each successful API call. For pricing, see Vertical Query - billing details.

  • This service requires activation. Contact your business manager for assistance. You can also apply for activation through the official Q&A channel: Q&A and consultation

2. API definition

Request parameters

Parameter

Type

Required

Description

engineType

string

Yes

Must be set to RecognizeWritten. The value is case-sensitive.

dataUrl

string or null

Mutually exclusive with dataBase64

A downloadable image URL. We recommend using a public HTTPS URL.

dataBase64

string or null

Mutually exclusive with dataUrl

The standard Base64 encoding of a single original image, without the data: prefix. The original image cannot exceed 10 MiB (10,485,760 bytes).

  1. You must provide a value for exactly one of dataUrl or dataBase64 as the image source. You can omit the unused field or set it to null, an empty string, or a string that contains only ASCII whitespace.

  2. The dataBase64 string can contain ASCII whitespace. After removing the whitespace, the string's length must be a multiple of 4, and it must include the required standard Base64 trailing padding. The size limit applies to the decoded bytes of the original image, not the length of the encoded string.

  3. This operation does not support inputConfig. The request must contain only the fields that are defined in the preceding table.

Response parameters

A successful response contains the following top-level fields.

Field

Type

Return rule

Description

requestId

string

Always returned

The request ID. Provide this ID when you submit a ticket for troubleshooting.

searchInformation

object

Always returned

Information about the document processing time.

searchInformation.searchTime

int64

Always returned

The document processing time, in milliseconds.

functionOption

string

Always returned

Always returns RecognizeWritten, which is identical to the engineType in the request.

resultData

string

Always returned

A string that contains a JSON object, not a top-level JSON object. After parsing the API response, you must parse this string as a second JSON object.

Fields in resultData

Field names are case-sensitive. All fields listed below are optional; the actual content depends on the image. Your client must be able to handle additional, unlisted fields. A missing field is not equivalent to an empty string or successful recognition. The OcrInfo field can also be null.

In the table, [i], [j], and [k] represent the indexes of arbitrary elements in each array layer and are used only to illustrate the field path, not as fixed indexes or actual returned field names.

Field

Type

Return rule

Description

OcrInfo

array<object> or null

Optional

A list of handwritten text and content blocks, or null.

OcrInfo[i].Text

string

Optional

The recognized text.

OcrInfo[i].Detail

array<object>

Optional

Detailed information about the recognized content.

OcrInfo[i].Detail[j].InGraph

boolean

Optional

A flag that indicates whether the content is related to a graphic.

OcrInfo[i].Detail[j].Type

string

Optional

A content type identifier. Handle this field based on its value.

OcrInfo[i].Detail[j].Confidence

number

Optional

The confidence score of the recognition result.

OcrInfo[i].Detail[j].Position

array<array<number>>

Optional

The position of the content, represented by the coordinates of its four vertices. This field is returned when the result includes a corrected image.

OcrInfo[i].Detail[j].Value

string

Optional

The recognized value of the detailed item.

ImageInfo

array<object>

Optional

A list of information about the recognized image. This field is located inside the parsed resultData.

ImageInfo[i].Angle

number

Optional

The angle of the image.

ImageInfo[i].Height

number

Optional

The height of the image in pixels.

ImageInfo[i].Width

number

Optional

The width of the image in pixels.

ImageInfo[i].ImageBase64

string

Optional

The Base64-encoded string of the image.

3. Example

The following example shows the data structure of a request and response. The image URL is a placeholder. Replace it with a valid, downloadable image URL before making a call. The recognition results may vary.

Request JSON:

{
  "engineType": "RecognizeWritten",
  "dataUrl": "https://example.com/images/RecognizeWritten.png"
}

Response JSON:

{
  "requestId": "example-request-id",
  "searchInformation": {
    "searchTime": 120
  },
  "functionOption": "RecognizeWritten",

  "resultData": "{\"OcrInfo\":[{\"Text\":\"Order A01\"}],\"ImageInfo\":[ ]}"

}

Result after parsing the resultData string:

{
  "OcrInfo": [
    {
      "Text": "Order A01"
    }
  ],

  "ImageInfo": [ ]

}

4. API call

Use an API key that is enabled for this operation. Read the credential from the API_KEY environment variable and pass it in the X-API-Key request header. Do not hardcode real credentials in front-end web pages or public code. The following curl example uses the same operation and image URL as the request example. Replace the placeholder image URL before you make an actual call.

curl -X POST 'https://cloud-iqs.aliyuncs.com/document/process' \
  --header "X-API-Key: $API_KEY" \
  --header 'Content-Type: application/json' \
  --data '{
    "engineType": "RecognizeWritten",
    "dataUrl": "https://example.com/images/RecognizeWritten.png"
  }'

When you use Base64, replace dataUrl with dataBase64 and pass the complete encoded string. Adhere to the rules for mutually exclusive image sources, padding, and original image size limits. The call method is still POST with a JSON body.

5. Error codes

First, check the HTTP status code and the format of the response body, and then read the error information. A JSON response for a document processing business error includes errorCode and errorMessage. The optional code and message fields have the same meaning as the first two. A 403 response due to permission or quota issues does not guarantee that all these fields are present. You should prioritize reading the errorCode and errorMessage fields. If they are missing, read the code and message fields instead. The requestId is not guaranteed in all error responses, and the response body may be non-JSON or empty.

Parameter error example:

{
  "errorCode": "InvalidParameter",
  "errorMessage": "Image too large",
  "code": "InvalidParameter",
  "message": "Image too large"
}

HTTP status

Error code

Description

Solution

400

MissingParameter

An image source or a required parameter is missing.

Check the engineType and provide one non-empty image source.

400

InvalidParameter

A parameter is invalid or the image is too large.

Check the case of engineType, the mutual exclusion rule for image sources, the Base64 encoding, and the image size. Remove unsupported inputConfig or other extra fields.

403

Retrieval.InvalidAPIKey

The API key is invalid.

Check the X-API-Key request header and the credential.

403

Varies by response

Insufficient permissions or quota.

Verify the service activation status, call permissions, and available quota.

415

Varies by response

Unsupported request format. The response body may be non-JSON or empty.

Use Content-Type: application/json.

429

Retrieval.Throttling.scan (common)

Requests are too frequent.

Reduce the call frequency and implement a backoff retry.

502

Varies by response

Failed to process the image or retrieve the result.

Check whether the image is downloadable and inspect its content. If the issue persists, provide the request ID to technical support.

503

Varies by response

The service is temporarily unavailable.

Retry later. If the issue persists, contact technical support.

500

Varies by response

An internal service error occurred.

Provide the request ID and the error response body to technical support.

If the case of engineType is incorrect (for example, recognizewritten instead of RecognizeWritten) or the JSON request body is truncated, the service may return a non-JSON text response. Do not attempt to parse all error responses as JSON.

Related documents