Bỏ qua

Using Project Access Token

Warning

The current page still doesn't have a translation for this language.

But you can help translating it: Contributing.

In this guide, we'll explore how to interact with DATAMIMIC projects programmatically using project access tokens. This approach allows you to manage your projects and generate data through API calls, which is particularly useful for automation and CI/CD pipelines.

Prerequisites

  • A DATAMIMIC project
  • curl installed on your system
  • jq installed on your system (for JSON processing)
  • base64 command-line utility

Getting Started

  1. Clone the Demo Basic JSON from the DATAMIMIC Demo Store.
  2. Add a new project access token for this newly clone demo project.
  3. Note down both the Project ID and the newly Project Access Token.

Basic Operations

Reading Project Files

To read the content of a file (e.g., datamimic.xml) and save it to a local file also call datamimic.xml:

1
2
3
4
5
curl -X 'GET' \
  'https://your-datamimic-instance/api/v1/{project-id}/file/read/datamimic.xml' \
  -H 'accept: application/json' \
  -H 'Authorization: Bearer {your-access-token}' | \
  jq -r | base64 --decode > datamimic.xml

Updating Project Files

Make any change you want to the file content locally then update it to the system.

To update a file's content:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
curl -X 'POST' \
  'https://your-datamimic-instance/api/v1/{project-id}/files/update' \
  -H 'accept: application/json' \
  -H 'Authorization: Bearer {your-access-token}' \
  -H 'Content-Type: application/json' \
  -d "$(jq -nc --arg code "$(base64 -w 0 -i datamimic.xml)" \
    '[{
      code: $code,
      encoding: "utf-8",
      extension: ".xml",
      index: 0,
      name: "datamimic",
      uri: "{project-id}/datamimic.xml"
    }]')"

Generating Data

To trigger data generation:

1
2
3
4
curl -X 'POST' \
  'https://your-datamimic-instance/api/v1/{project-id}/generate' \
  -H 'accept: application/json' \
  -H 'Authorization: Bearer {your-access-token}'

This call will return a Task ID that you can use to track the generation progress and download artifacts.

Downloading Generated Data

To download and view generated artifacts:

1
2
3
curl -X 'GET' \
  'https://your-datamimic-instance/api/v1/{project-id}/task/{task-id}/artifact/download/person/json' \
  -o person.json && jq . person.json

Example Workflow

Here's a complete example of a typical workflow:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Set your variables
PROJECT_ID="your-project-id"
ACCESS_TOKEN="your-access-token"
API_BASE="https://your-datamimic-instance/api/v1"

# Read current configuration
curl -X 'GET' \
  "${API_BASE}/${PROJECT_ID}/file/read/datamimic.xml" \
  -H "Authorization: Bearer ${ACCESS_TOKEN}" | \
  jq -r | base64 --decode > datamimic.xml

# Make changes to datamimic.xml as needed

# Update configuration
curl -X 'POST' \
  "${API_BASE}/${PROJECT_ID}/files/update" \
  -H "Authorization: Bearer ${ACCESS_TOKEN}" \
  -H 'Content-Type: application/json' \
  -d "$(jq -nc --arg code "$(base64 -w 0 -i datamimic.xml)" \
    '[{
      code: $code,
      encoding: "utf-8",
      extension: ".xml",
      index: 0,
      name: "datamimic",
      uri: "'${PROJECT_ID}'/datamimic.xml"
    }]')"

# Generate data
TASK_ID=$(curl -X 'POST' \
  "${API_BASE}/${PROJECT_ID}/generate" \
  -H "Authorization: Bearer ${ACCESS_TOKEN}" | \
  jq -r '.task_id')

# Download and view results
curl -X 'GET' \
  "${API_BASE}/${PROJECT_ID}/task/${TASK_ID}/artifact/download/person/json" \
  -o person.json && jq . person.json

Note

Replace your-datamimic-instance, {project-id}, {task-id} and {your-access-token} with your actual values in all examples.

Recap

In this guide, we explored how to programmatically interact with DATAMIMIC projects using project access tokens. Here's a summary of the key concepts and operations:

  1. Setup and Authentication:

    • Created/used a DATAMIMIC project
    • Generated a project access token
    • Used the token for API authentication
  2. Basic Operations:

    • Reading project files using GET requests
    • Updating file content using POST requests
    • Triggering data generation
    • Downloading and viewing generated artifacts
  3. Workflow Integration:

    • Learned how to combine operations into a complete workflow
    • Used environment variables for configuration
    • Implemented basic automation patterns

These API operations enable you to automate DATAMIMIC project management and integrate it into your development workflows, CI/CD pipelines, or custom applications.