Turkish ID Info Verification with Python

Fatih Koprucu
turkcell
Published in
4 min readSep 26, 2022

If your work is related to any kind of ID information, you know that most of the users are not keen to give the actual info. Then you need to verify these info before any saving operation.

There will be series of posts consists of ID verification, serverless architecture deployment with REST API of this verification and OCR implementation to gather values from ID image. But this post is focusing on the ID info validation.

There are various API’s for verification purpose depends on the type of ID. For Turkish ID, the government provides a SOAP API to validate ID info. You can check it out from here

Simply it takes name, surname, birth year and ID number fields and returns a Boolean value. As you can imagine, it returns true if the arguments are accurate.

This is the sample request:

Name, surname, ID number and birth year values needs to be provided in the fields Ad, Soyad, TCKimlikNo and DogumYili respectively.

Let’s code…

Pre-Request ID Number Validation

Before we start to prepare the SOAP request, we should validate the format of the ID number. There is a certain validation algorithm for Turkish ID number. These are:

  1. The ID number’s should consist of 11 characters
  2. It can’t be started with ‘0’
  3. If we sum the numbers at the (1,3,5,7) indexes and multiply it with 7 then subtract the sum of the numbers at the (2,4,6,8); modulus 10 of the result must be the number at the index 10 (I am serious :)).
  4. Finally the modulus 10 of the sum of the numbers at the index from 1 to 10 is equals to the number at the index 11

So I want to validate the ID number before requesting the API. If the format is not correct then I should return false without sending the request.

I want to start coding with these validation methods.

The ID number’s should consist of 11 characters

Just a simple length check is enough for this:

It can’t be started with ‘0’

We can apply list operations to the string values such as:

If we sum the numbers at the (1,3,5,7) indexes and multiply it with 7 then subtract the sum of the numbers at the (2,4,6,8); modulus 10 of the result must be the number at the index 10

Things are getting messy after that. So I think we need a method to gather sum of the numbers at any indexes:

Now we can write that complicated validation method to control 10th digit:

Remember the indexes is started from 0 not 1 !!!

I used range here to get sequence of the numbers such as: range(0,10,2) means: start from 0 and add 2 on each step while the upper limit is 10 but not included. So it is (0,2,4,6,8).

The sum of the numbers at the index from 1 to 10 is equals to the number at the index 11

This one is easier compare the previous. We just get the sum of the numbers at the index from 1 to 10 then check whether the sum is equal to number at the index 11. The validation method is:

The final form of the pre-request validation (is_id_no_valid) method is like that:

Sending the SOAP Request

We will use urllib module to send the request.

I created a method called is_info_valid which takes id_no, name, surname and birth_year arguments. Then I formatted the XML Request template and provide the fields with that arguments to create the payload. The payload was encoded with UTF-8 in order to get rid of the problems about the Turkish characters. At the header I specified contentType as text/xml:

Now the request configs are ready. It is time to send the SOAP request. I will provide URL, headers and formatted payload for the request then encode the SOAP response:

If you check the SOAP response defined here, It is described as: <TCKimlikNoDogrulaResult>boolean</TCKimlikNoDogrulaResult>. Since I am a lazy developer, I splitted the text with <TCKimlikNoDogrulaResult> and check the value after that. I know this can be solved with something more specific like XML parser or regex but I am not focusing on that so I just go with brute-force solution :)

The final version of the is_info_valid method is such as:

The verification process starts with ID number argument format verification, if the ID number is valid then we send the SOAP request and check for the response. So the main validate_id_info method consist of these two verification:

To sum all these up together, the final form of the code:

All you need to to is call the validate_id_info method with the respective arguments. Then it will return a boolean value: True if the id info is accurate.

Next Steps

For the next study; I will post a paper that describes how to deploy this ID verification code to AWS Lambda. I will explain the serverless architecture and create a REST API with AWS Lambda and based on this post.

After that, I am planning to implement an OCR model to gather ID info from the Turkish ID image to initiate the process.

You can find the github repo for this code here

I hope you enjoyed the post

Cheers :)

--

--