Creating a standardized request and response JSON schema is essential for ensuring consistency, scalability, and ease of integration across your API marketplace. Below, I'll provide comprehensive and professional JSON schemas for both requests and responses that can be adapted to various Indian fintech APIs such as PAN verification, Aadhar verification, face match, and liveness APIs.
1. Overview
Key Features of the Standardized Schema:
- Consistency: Uniform structure across all APIs.
- Flexibility: Ability to include API-specific data.
- Security: Incorporation of authentication mechanisms.
- Comprehensive Error Handling: Standardized error responses.
- Versioning: Support for API version management.
2. Standardized Request JSON Schema
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "API Request Schema",
"type": "object",
"required": ["apiVersion", "apiName", "requestId", "timestamp", "auth", "payload"],
"properties": {
"apiVersion": {
"type": "string",
"description": "Version of the API being called, e.g., v1, v2."
},
"apiName": {
"type": "string",
"description": "Name of the API, e.g., PANVerification, AadharVerification."
},
"requestId": {
"type": "string",
"description": "Unique identifier for the request to trace and debug."
},
"timestamp": {
"type": "string",
"format": "date-time",
"description": "ISO 8601 timestamp of when the request was made."
},
"auth": {
"type": "object",
"required": ["apiKey"],
"properties": {
"apiKey": {
"type": "string",
"description": "API key provided to the client for authentication."
},
"secretKey": {
"type": "string",
"description": "Secret key for additional security (optional, based on security requirements)."
}
},
"additionalProperties": false
},
"payload": {
"type": "object",
"description": "API-specific data payload.",
"properties": {
// Define payload properties based on the API
// Example for PAN Verification:
"panNumber": {
"type": "string",
"pattern": "^[A-Z]{5}[0-9]{4}[A-Z]{1}$",
"description": "PAN number to be verified."
},
// Add other fields as necessary for different APIs
},
"additionalProperties": false
}
},
"additionalProperties": false
}
Explanation of Request Schema Fields:
- apiVersion: Helps in version management and backward compatibility.
- apiName: Identifies which API the request is targeting.
- requestId: Facilitates tracking and debugging of requests.
- timestamp: Records when the request was made, useful for logging and security.
- auth: Contains authentication details to secure the API.
- apiKey: Mandatory for authenticating the client.
- secretKey: Optional additional security layer.
- payload: Contains the actual data required by the specific API.
- Fields within
payload should be defined based on the API's requirements.
3. Standardized Response JSON Schema
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "API Response Schema",
"type": "object",
"required": ["requestId", "timestamp", "status", "response"],
"properties": {
"requestId": {
"type": "string",
"description": "Echoes the requestId from the request for correlation."
},
"timestamp": {
"type": "string",
"format": "date-time",
"description": "ISO 8601 timestamp of when the response was generated."
},
"status": {
"type": "object",
"required": ["code", "message"],
"properties": {
"code": {
"type": "integer",
"description": "HTTP-like status code representing the outcome."
},
"message": {
"type": "string",
"description": "Human-readable status message."
}
},
"additionalProperties": false
},
"response": {
"type": "object",
"description": "API-specific response data.",
"properties": {
// Define response properties based on the API
// Example for PAN Verification:
"valid": {
"type": "boolean",
"description": "Indicates whether the PAN number is valid."
},
"holderName": {
"type": "string",
"description": "Name of the PAN holder."
},
// Add other fields as necessary for different APIs
},
"additionalProperties": false
},
"errors": {
"type": "array",
"description": "List of errors if the request failed.",
"items": {
"type": "object",
"required": ["code", "message"],
"properties": {
"code": {
"type": "string",
"description": "Error code identifying the type of error."
},
"message": {
"type": "string",
"description": "Detailed error message."
},
"field": {
"type": ["string", "null"],
"description": "The specific field related to the error, if applicable."
}
},
"additionalProperties": false
},
"minItems": 1
}
},
"additionalProperties": false,
"allOf": [
{
"if": {
"properties": { "status": { "properties": { "code": { "const": 200 } } } }
},
"then": {
"required": ["response"],
"properties": {
"errors": {
"not": {}
}
}
},
"else": {
"required": ["errors"],
"properties": {
"response": {
"not": {}
}
}
}
}
]
}
Explanation of Response Schema Fields:
- requestId: Helps in correlating responses with requests.
- timestamp: Indicates when the response was generated.
- status:
- code: Follows HTTP-like status codes (e.g., 200 for success, 400 for client errors).
- message: Descriptive message corresponding to the status code.
- response: Contains the actual data returned by the API.
- Fields within
response should be defined based on the API's response structure.
- errors: Contains error details if the request failed.
- code: Specific error code.
- message: Descriptive error message.
- field: (Optional) Indicates the related field causing the error.
Conditional Requirements:
- If the
status.code is 200, the response object is required, and errors should not be present.
- For all other status codes, the
errors array is required, and response should not be present.
4. Examples for Specific APIs
a. PAN Verification
Request Example
{
"apiVersion": "v1",
"apiName": "PANVerification",
"requestId": "req-123456789",
"timestamp": "2023-10-10T12:34:56Z",
"auth": {
"apiKey": "your-api-key"
},
"payload": {
"panNumber": "ABCDE1234F"
}
}
Response Example
Success Response
{
"requestId": "req-123456789",
"timestamp": "2023-10-10T12:35:00Z",
"status": {
"code": 200,
"message": "PAN verification successful."
},
"response": {
"valid": true,
"holderName": "John Doe"
}
}
Error Response
{
"requestId": "req-123456789",
"timestamp": "2023-10-10T12:35:00Z",
"status": {
"code": 400,
"message": "Invalid PAN number format."
},
"errors": [
{
"code": "INVALID_FORMAT",
"message": "PAN number does not match the required pattern.",
"field": "panNumber"
}
]
}
b. Aadhar Verification
Request Example
{
"apiVersion": "v1",
"apiName": "AadharVerification",
"requestId": "req-987654321",
"timestamp": "2023-10-10T13:00:00Z",
"auth": {
"apiKey": "your-api-key"
},
"payload": {
"aadharNumber": "123412341234"
}
}
Response Example
Success Response
{
"requestId": "req-987654321",
"timestamp": "2023-10-10T13:00:05Z",
"status": {
"code": 200,
"message": "Aadhar verification successful."
},
"response": {
"valid": true,
"name": "Jane Smith",
"dob": "1990-01-01",
"gender": "Female"
}
}
Error Response
{
"requestId": "req-987654321",
"timestamp": "2023-10-10T13:00:05Z",
"status": {
"code": 404,
"message": "Aadhar number not found."
},
"errors": [
{
"code": "NOT_FOUND",
"message": "No records found for the provided Aadhar number.",
"field": "aadharNumber"
}
]
}
5. Best Practices
-
Use Consistent Naming Conventions: Follow a consistent naming strategy (e.g., camelCase) for all JSON keys.
-
Version Your APIs: Including apiVersion in your request allows for smooth transitions between different versions of your APIs without breaking existing integrations.
-
Secure Your APIs:
- Authentication: Use API keys, OAuth tokens, or other secure methods.
- HTTPS: Ensure all API communications occur over HTTPS to encrypt data in transit.
- Rate Limiting: Implement rate limiting to prevent abuse.
-
Comprehensive Error Handling:
- Provide meaningful error codes and messages.
- Avoid exposing sensitive information in error responses.
- Clearly indicate which fields caused the error.
-
Documentation:
- Maintain detailed API documentation outlining the request and response structures for each API.
- Include examples for different scenarios (success, various errors).
-
Validation:
- Implement server-side validation to ensure requests adhere to the schema.
- Use schema validation tools or libraries to automate this process.
-
Extensibility:
- Allow for future enhancements without breaking existing clients by using
additionalProperties judiciously or versioning your APIs.
-
Monitoring and Logging:
- Log all requests and responses (while adhering to privacy regulations) for monitoring and debugging purposes.
- Implement alerting mechanisms for unusual activities or errors.
6. Conclusion
Adopting a standardized JSON schema for your API gateway will greatly enhance the developer experience, simplify maintenance, and ensure scalability as your API marketplace grows. The provided schemas are designed to be robust and flexible, catering to various fintech APIs while maintaining consistency across the platform. Remember to tailor the payload and response sections to fit the specific requirements of each API you host.
If you require further customization or have specific needs, consider leveraging API specification frameworks like OpenAPI Specification (OAS) or RAML for more advanced features and tooling support.