API Academy
🌐 English
  • 🌐 English
  • 🌐 繁體中文
HomePetstore APIExplore more APIs
HomePetstore APIExplore more APIs
🌐 English
  • 🌐 English
  • 🌐 繁體中文
🌐 English
  • 🌐 English
  • 🌐 繁體中文
  1. Designing APIs
  • Introduction
  • Table of Contents
  • API Academy
    • Get Started
      • What is an API?
      • How Does an API Work?
      • How to Call an API?
      • How to Read an API Documentation?
      • Chapter Summary
      • Get realtime weather
    • API Fundamentals
      • API Funtamentals: Overview
      • Method & Path
      • Parameters
      • Request Body
      • Responses
      • API Specification & OAS
      • Chapter Summary
    • Working with APIs
      • Working with APIs: Overview
      • Making Requests from Spec
      • Environments and Variables
      • Chaining Multiple Endpoints
      • Handling Authentication
      • Handling API Signatures
      • Introduction to Scripts
      • Chapter Summary
    • Mocking APIs
      • Mocking APIs: Overview
      • Smart Mock
      • Mock Expectations
      • Cloud Mock
      • Mock Scripts
      • Chapter Summary
    • Designing APIs
      • Designing APIs: Overview
      • Introduction to API Design
      • Creating Your First API Project
      • Analyzing Requirements and Planning Your API
      • Designing Data Models
      • Designing Endpoints
      • Using Components and Reusability
      • Setting Up Authentication
      • API Design Guidelines
      • Chapter Summary
    • Developing APIs
      • Developing APIs: Overview
      • Setup: Install Your AI Coding Assistant
      • Quick Start: From Spec to Running API in 30 Minutes
      • Understanding the Generated Code
      • Testing Your API with Apidog
      • Deployment: Put Your API Online
      • Chapter Summary
    • Testing APIs
      • Testing APIs: Overview
      • Getting Started: Your First Test Scenario
      • Integration Testing and Data Passing
      • Dynamic Values
      • Assertions and Validations
      • Flow Control: If, For, ForEach
      • Data-Driven Testing
      • Performance Testing
      • Test Reports and Analysis
      • CI/CD Integration
      • Scheduled Tasks and Automation
      • Advanced Testing Strategies
      • Chapter Summary
    • API Documentations
      • API Documentations: Overview
      • Publishing Your First API Doc
      • Customizing Documentation Appearance
      • Interactive Features for Consumers
      • Advanced Publishing Settings
      • Managing API Versions
      • Chapter Summary
    • Advanced API Technologies
      • API Technologies: Overview
      • GraphQL
      • gRPC
      • WebSocket
      • Socket.IO
      • Server-Sent Events (SSE)
      • SOAP
      • Chapter Summary
    • API Lifecycle
      • API Lifecycle: Overview
      • Stages of the API Lifecycle
      • API Governance
      • API Security Best Practices
      • Monitoring and Analytics
      • API Versioning Strategies
      • The Future of APIs
      • Chapter Summary
    • API Security
      • API Security: Overview
      • API Security Fundamentals
      • Authentication vs Authorization
      • Understanding OAuth 2.0 and OpenID Connect
      • JSON Web Tokens (JWT)
      • OWASP API Security Top 10
      • Encryption and HTTPS
      • Chapter Summary
    • API Tools
      • API Tools: Overview
      • The Evolution of API Tools
      • API Clients
      • Command Line Tools (cURL, HTTPie)
      • API Design and Documentation Tools
      • API Mocking Tools
      • API Testing Tools
      • All-in-One API Platforms
      • Chapter Summary
    • API Gateway
      • API Gateway: Overview
      • What is an API Gateway?
      • Key Features of API Gateways
      • API Gateway vs Load Balancer vs Service Mesh
      • Popular API Gateway Solutions
      • The BFF (Backend for Frontend) Pattern
      • Chapter Summary
  • Modern Pet Store
    • Pet
      • Get Pet
      • Update Pet
      • Delete Pet
      • Create Pet
      • List Pets
      • Upload Pet Image
    • User
      • Update User
      • Get User
      • Delete User
      • Login
      • Logout
      • Create User
    • Store
      • List Inventory
      • Create Order
      • Get Order
      • Delete Order
      • Callback Example
      • Pay for an Order
    • Payments
      • Pay Order
    • Chat
      • Create Chat Completion
    • Webhooks
      • Pet Adopted Event
      • New Pet Available Event
  • Schemas
    • Pet
    • Category
    • User
    • ApiResponse
    • OrderPayment
    • Tag
    • Order
    • Links-Order
    • PetCollection
    • Bank Card
    • Bank Account
    • Links
    • Error
HomePetstore APIExplore more APIs
HomePetstore APIExplore more APIs
🌐 English
  • 🌐 English
  • 🌐 繁體中文
🌐 English
  • 🌐 English
  • 🌐 繁體中文
  1. Designing APIs

Designing Data Models

In the previous chapter, you identified what data your API needs. Now it's time to design the data models and implement them as schemas in Apidog.
Data models define the structure of your API's dataβ€”what fields exist, their types, validation rules, and how they're used. Schemas are the technical implementation of these models in Apidog using JSON Schema.
In this chapter, we'll learn how to design data models and immediately implement them as schemas in Apidog. We'll use the Pet Store User module as our example, creating schemas step by step.

1. What Are Data Models and Schemas?#

A data model is a conceptual design of how your data is structured. A schema is the technical specification that implements that model.
Think of it this way:
Data Model = the blueprint (what you design)
Schema = the actual specification in Apidog/OpenAPI (what you implement)
In Apidog, schemas are based on JSON Schema and define:
Field names and types
Required vs optional fields
Validation rules (format, length, patterns)
Nested objects and arrays
Default values

2. The Design Process#

When designing data models, follow this approach:
1.
Start with requirements β€” What data do you need? (from previous chapter)
2.
Design the model β€” What fields, types, and rules?
3.
Implement as schema β€” Create it in Apidog
4.
Test and refine β€” Validate the design
Let's apply this to the User module.

3. Designing the User Model#

Based on our requirements analysis, a User needs:
Required fields:
id β€” unique identifier (generated by system)
email β€” for login and communication
firstName β€” user's first name
lastName β€” user's last name
createdAt β€” account creation timestamp
Optional fields:
phone β€” phone number
preferences β€” user preferences (nested object)
Authentication:
password β€” for login (never returned in responses)

User Model Structure#

Here's what the User model looks like:
{
  "id": "usr_3Oy2JIS7TMJgEXfM",
  "email": "jane.smith@example.com",
  "firstName": "Jane",
  "lastName": "Smith",
  "phone": "+14155551234",
  "preferences": {
    "newsletter": true,
    "notifications": false
  },
  "createdAt": "2024-01-15T10:30:00Z"
}

4. Creating Schemas in Apidog#

Now let's implement this model as a schema in Apidog. Apidog's recommended approach is to paste JSON directly, and it will automatically recognize and generate the schema structure.

Step 1: Create the User Schema#

1.
In your Apidog project, go to the Schemas section
2.
Click the βž• icon and select New Schema
3.
Name it "User" and add a description: "User account information"

Step 2: Generate Schema from JSON#

Instead of manually adding properties one by one, Apidog allows you to paste JSON and automatically generate the schema:
1.
Click "Generate from JSON etc." button in the Schema Editor
2.
Paste the following JSON:
{
  "id": "usr_3Oy2JIS7TMJgEXfM",
  "email": "jane.smith@example.com",
  "firstName": "Jane",
  "lastName": "Smith",
  "phone": "+14155551234",
  "preferences": {
    "newsletter": true,
    "notifications": false
  },
  "createdAt": "2024-01-15T10:30:00Z"
}
3.
Apidog will automatically:
Recognize all properties and their types
Set appropriate data types (string, boolean, object, date-time)
Create the nested preferences object structure
Note: By default, all properties in the JSON will be marked as required. You'll need to adjust this manually for optional fields.

Step 3: Refine the Generated Schema#

After generating from JSON, refine the schema:
1.
Mark optional fields as not required:
Uncheck "Required" for phone and preferences
2.
Add validation rules:
id: Add pattern ^usr_[A-Za-z0-9]{16}$ and mark as read-only
email: Set format to email, add max length 255
firstName and lastName: Add max length 50
phone: Add pattern ^\+?[1-9]\d{1,14}$ (E.164 format)
createdAt: Set format to date-time and mark as read-only
3.
Add descriptions to each field for better documentation
Tip: Apidog's JSON smart recognition generates the data structure based on the JSON values, but doesn't save the actual values. You can add descriptions and adjust validation rules afterward.

5. Creating Nested Schemas: UserPreferences#

The preferences field is a nested object. Let's create a separate schema for it so we can reuse it.

Step 1: Create UserPreferences Schema#

1.
Create a new schema named "UserPreferences"
2.
Add description: "User communication preferences"

Step 2: Generate from JSON#

1.
Click "Generate from JSON etc."
2.
Paste this JSON:
{
  "newsletter": true,
  "notifications": false
}
3.
Apidog will automatically recognize both boolean fields

Step 3: Refine the Schema#

1.
Mark both fields as optional (uncheck "Required")
2.
Set default values:
newsletter: Default false
notifications: Default true
3.
Add descriptions to each field

Step 4: Reference in User Schema#

Now go back to the User schema and:
1.
Select the preferences property
2.
Change its type to Reference
3.
Select UserPreferences from the list
This creates a reference, allowing you to reuse the UserPreferences schema. If you update UserPreferences later, all references will automatically reflect the changes.

6. Schema Best Practices#

When designing schemas, keep these principles in mind:

Keep It Simple#

Avoid over-engineering: Don't add fields "just in case"
Use simple types when possible: Prefer string over complex objects when appropriate
Group related fields: Use nested objects for logical groupings (like preferences)

Avoid Over-Nesting#

Try to keep nesting to 2-3 levels maximum. Deep nesting makes schemas hard to understand and use.

Use Schema References#

When you have reusable structures (like UserPreferences), create separate schemas and reference them. This:
Reduces duplication
Makes updates easier
Improves consistency

Security Considerations#

Never include passwords in response schemas
Use write-only for sensitive input fields (password)
Use read-only for system-generated fields (id, createdAt)
Validate all input according to your rules

Plan for Extensibility#

Use optional fields for new features
Avoid removing fields (mark as deprecated instead)
Use API versioning for major changes

7. Key Takeaways#

Designing and implementing data models is a crucial step:
1.
Data models define what data your API uses
2.
Schemas are the technical implementation in Apidog
3.
Use "Generate from JSON" to quickly create schemas from JSON data
4.
Use schema references for reusable structures (like UserPreferences)
5.
Create core schemas first β€” request/response variations can be handled at the endpoint level
6.
Security matters: Use write-only for sensitive fields, read-only for system-generated fields
7.
Keep it simple: Avoid over-nesting and over-engineering

Now that you have your core schemas designed and implemented, you're ready to start building endpoints. In the next chapter, we'll learn how to design endpoints in Apidog, using the schemas we created and the endpoint structure we planned earlier.
Let's continue with Designing Endpoints.
Modified atΒ 2025-12-25 09:47:19
Previous
Analyzing Requirements and Planning Your API
Next
Designing Endpoints
Built with