API Reference
Core Classes
ThaiIDCardReader
pythaiidcard.reader.ThaiIDCardReader
Reader for Thai National ID Cards.
Source code in pythaiidcard/reader.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | |
Functions
__init__(reader_index=None, retry_count=3, skip_system_check=False)
Initialize the Thai ID Card reader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reader_index
|
Optional[int]
|
Index of the reader to use (None for auto-select) |
None
|
retry_count
|
int
|
Number of retries for failed operations |
3
|
skip_system_check
|
bool
|
Skip system dependency check (default: False) |
False
|
Raises:
| Type | Description |
|---|---|
SystemDependencyError
|
If required system dependencies are missing |
Source code in pythaiidcard/reader.py
list_readers(skip_system_check=False)
staticmethod
List all available smart card readers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
skip_system_check
|
bool
|
Skip system dependency check (default: False) |
False
|
Returns:
| Type | Description |
|---|---|
List[CardReaderInfo]
|
List of CardReaderInfo objects |
Raises:
| Type | Description |
|---|---|
NoReaderFoundError
|
If no readers are found |
SystemDependencyError
|
If required system dependencies are missing |
Source code in pythaiidcard/reader.py
connect()
Connect to the Thai ID card.
Raises:
| Type | Description |
|---|---|
NoCardDetectedError
|
If no card is detected |
CardConnectionError
|
If connection fails |
InvalidCardError
|
If card is not a Thai ID card |
Source code in pythaiidcard/reader.py
disconnect()
Disconnect from the card.
Source code in pythaiidcard/reader.py
read_card(include_photo=True, photo_progress_callback=None)
Read all data from Thai ID card.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_photo
|
bool
|
Whether to read the photo |
True
|
photo_progress_callback
|
Optional[callable]
|
Optional callback for photo reading progress |
None
|
Returns:
| Type | Description |
|---|---|
ThaiIDCard
|
ThaiIDCard object with all card data |
Raises:
| Type | Description |
|---|---|
CardConnectionError
|
If not connected to card |
DataReadError
|
If reading any field fails |
CommandError
|
If APDU command fails |
Source code in pythaiidcard/reader.py
card_session()
Context manager for card operations.
Usage
with reader.card_session(): card = reader.read_card()
Source code in pythaiidcard/reader.py
ThaiIDCard
pythaiidcard.models.ThaiIDCard
Bases: BaseModel
Model representing data from a Thai National ID Card.
Source code in pythaiidcard/models.py
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | |
Attributes
cid = Field(..., description='13-digit citizen identification number', min_length=13, max_length=13, pattern='^\\d{13}$')
class-attribute
instance-attribute
thai_fullname
property
Get Thai full name as string (backward compatibility).
english_fullname
property
Get English full name as string (backward compatibility).
date_of_birth = Field(..., description='Date of birth')
class-attribute
instance-attribute
gender = Field(..., description='Gender (1=Male, 2=Female)', pattern='^[12]$')
class-attribute
instance-attribute
gender_text
property
Get gender as text.
card_issuer = Field(..., description='Card issuing organization')
class-attribute
instance-attribute
issue_date = Field(..., description='Date when the card was issued')
class-attribute
instance-attribute
expire_date = Field(..., description='Date when the card expires')
class-attribute
instance-attribute
address
property
Get full address as string (backward compatibility).
photo = Field(None, description='JPEG photo data')
class-attribute
instance-attribute
age
property
Calculate current age from date of birth.
is_expired
property
Check if the card has expired.
days_until_expiry
property
Calculate days until card expiry.
Functions
save_photo(path=None)
Save photo to file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Optional[Path]
|
Path to save the photo. If None, saves as {cid}.jpg |
None
|
Returns:
| Type | Description |
|---|---|
Optional[Path]
|
Path where photo was saved, or None if no photo data |
Source code in pythaiidcard/models.py
CardReaderInfo
pythaiidcard.models.CardReaderInfo
Bases: BaseModel
Information about a smart card reader.
Source code in pythaiidcard/models.py
Convenience Functions
read_thai_id_card
pythaiidcard.reader.read_thai_id_card(reader_index=None, include_photo=True)
Convenience function to read a Thai ID card.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reader_index
|
Optional[int]
|
Reader index to use (None for auto-select) |
None
|
include_photo
|
bool
|
Whether to include photo data |
True
|
Returns:
| Type | Description |
|---|---|
ThaiIDCard
|
ThaiIDCard object with card data |
Raises:
| Type | Description |
|---|---|
SystemDependencyError
|
If system dependencies are missing |
NoReaderFoundError
|
If no readers are found |
NoCardDetectedError
|
If no card is detected |
CardConnectionError
|
If connection fails |
InvalidCardError
|
If not a Thai ID card |
DataReadError
|
If reading data fails |
Source code in pythaiidcard/reader.py
Utility Functions
CID Utilities
pythaiidcard.utils.validate_cid(cid)
Validate Thai citizen ID with checksum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cid
|
str
|
13-digit citizen ID |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if valid, False otherwise |
Source code in pythaiidcard/utils.py
pythaiidcard.utils.format_cid(cid)
Format citizen ID with dashes for readability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cid
|
str
|
13-digit citizen ID |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted CID (e.g., 1-2345-67890-12-3) |
Source code in pythaiidcard/utils.py
Date Utilities
pythaiidcard.utils.parse_buddhist_date(date_str)
Parse Buddhist Era date string to Gregorian.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date_str
|
str
|
Date in YYYYMMDD format (Buddhist Era) |
required |
Returns:
| Type | Description |
|---|---|
str
|
Date in YYYY-MM-DD format (Gregorian) |
Source code in pythaiidcard/utils.py
pythaiidcard.utils.calculate_age(birth_date_str)
Calculate age from Buddhist Era birth date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
birth_date_str
|
str
|
Birth date in YYYYMMDD format (Buddhist Era) |
required |
Returns:
| Type | Description |
|---|---|
Optional[int]
|
Age in years, or None if invalid date |
Source code in pythaiidcard/utils.py
pythaiidcard.utils.is_card_expired(expire_date_str)
Check if card has expired based on Buddhist Era date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expire_date_str
|
str
|
Expiry date in YYYYMMDD format (Buddhist Era) |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if expired, False otherwise |
Source code in pythaiidcard/utils.py
Text Utilities
pythaiidcard.utils.thai_to_unicode(data)
Convert Thai TIS-620 encoded data to Unicode string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes | List[int]
|
Bytes or list of integers in TIS-620 encoding |
required |
Returns:
| Type | Description |
|---|---|
str
|
Decoded Unicode string with # replaced by spaces and trimmed |
Source code in pythaiidcard/utils.py
pythaiidcard.utils.format_address(address)
Format address for better readability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address
|
str
|
Raw address string |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted address with proper spacing |
Source code in pythaiidcard/utils.py
Exceptions
ThaiIDCardException
pythaiidcard.exceptions.ThaiIDCardException
SystemDependencyError
pythaiidcard.exceptions.SystemDependencyError
Bases: ThaiIDCardException
Raised when required system dependencies are missing.
Source code in pythaiidcard/exceptions.py
NoReaderFoundError
pythaiidcard.exceptions.NoReaderFoundError
Bases: ThaiIDCardException
Raised when no smart card reader is detected.
Source code in pythaiidcard/exceptions.py
NoCardDetectedError
pythaiidcard.exceptions.NoCardDetectedError
Bases: ThaiIDCardException
Raised when no card is inserted in the reader.
Source code in pythaiidcard/exceptions.py
CardConnectionError
pythaiidcard.exceptions.CardConnectionError
Bases: ThaiIDCardException
Raised when connection to the card fails.
Source code in pythaiidcard/exceptions.py
InvalidCardError
pythaiidcard.exceptions.InvalidCardError
Bases: ThaiIDCardException
Raised when the card is not a valid Thai ID card.
Source code in pythaiidcard/exceptions.py
CommandError
pythaiidcard.exceptions.CommandError
Bases: ThaiIDCardException
Raised when an APDU command fails.
Source code in pythaiidcard/exceptions.py
DataReadError
pythaiidcard.exceptions.DataReadError
Bases: ThaiIDCardException
Raised when reading data from the card fails.
Source code in pythaiidcard/exceptions.py
InvalidReaderIndexError
pythaiidcard.exceptions.InvalidReaderIndexError
Bases: ThaiIDCardException
Raised when an invalid reader index is specified.
Source code in pythaiidcard/exceptions.py
CardTimeoutError
pythaiidcard.exceptions.CardTimeoutError
Bases: ThaiIDCardException
Raised when card operation times out.
Source code in pythaiidcard/exceptions.py
System Check
check_system_dependencies
pythaiidcard.system_check.check_system_dependencies(skip_check=False)
Check if all required system dependencies are installed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
skip_check
|
bool
|
If True, skip the check and return empty results |
False
|
Returns:
| Type | Description |
|---|---|
Dict[str, List[SystemDependency]]
|
Dict with 'missing' and 'installed' lists of dependencies |
Source code in pythaiidcard/system_check.py
check_and_raise_if_missing
pythaiidcard.system_check.check_and_raise_if_missing(skip_check=False)
Check system dependencies and raise exception if any are missing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
skip_check
|
bool
|
If True, skip the check entirely |
False
|
Raises:
| Type | Description |
|---|---|
SystemDependencyError
|
If required dependencies are missing |
Source code in pythaiidcard/system_check.py
Constants
APDUCommand
pythaiidcard.constants.APDUCommand
Base class for APDU commands.
Source code in pythaiidcard/constants.py
CardCommands
pythaiidcard.constants.CardCommands
APDU commands for Thai National ID Card operations.
Source code in pythaiidcard/constants.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
Functions
get_read_request(atr)
classmethod
Get the appropriate read request based on ATR.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atr
|
List[int]
|
Answer to Reset bytes |
required |
Returns:
| Type | Description |
|---|---|
List[int]
|
Read request command bytes |
Source code in pythaiidcard/constants.py
ResponseStatus
pythaiidcard.constants.ResponseStatus
Bases: Enum
Smart card response status codes.
Source code in pythaiidcard/constants.py
Functions
is_success(sw1, sw2)
classmethod
Check if response indicates success.
Returns True for: - 90 00: Success - 61 XX: Success with more data available