Skip to content

Build an HTTP Service With WSO2 Copilot

In this tutorial, you’ll create an HTTP service to add key-value pairs to a Redis database. The integrated AI-assistant will help you generate the integration flow.

Prerequisites

  • Docker installed on your machine.

Step 1: Create a new integration project

  1. Click on the Ballerina Integrator icon on the sidebar.
  2. Click on the Create New Integration button.
  3. Enter the project name as RedisService.
  4. Select project directory location by clicking on the Select Location button.
  5. Click on the Create New Integration button to create the integration project.

Step 2: Create a new integration

  1. In the design view click on the Generate with AI button.
  2. Enter the following prompt and press Enter:

     Create an integration service with a base path of /cache and a POST resource at /add that accepts key-value pairs and adds them to Redis.
    

    AI Prompt

  3. Click on + Add to Ingeration button to add the generated integration to the project.

  4. The generated integration will look like below:

    Generated integration

Step 3: Add a resource to get value

  1. Add the following prompt and press Enter:
     Add a resource to get the value of a key from Redis.
    
  2. Click on + Add to Ingeration button to add the generated integration to the project.
  3. The generated integration will look like below:

    Add get resource

Step 4: Start the Redis server

  1. Start the Redis server by running the following command:
    docker run --name some-redis -d -p 6379:6379 redis
    
  2. The redis server will start on port 6379 without password protection.

    Start Redis

Step 6: Configure the Redis client

  1. In the Integrator overview, click on the Configurations.
  2. Set redisHost value to localhost.
  3. Set redisPort value to 6379.

    Configurations

Step 5: Generate the curl commands

  1. Add the following prompt and press Enter to generate the curl command to add key-value pairs to the Redis server.:

     Generate a curl command to add key-value pairs to the Redis server.
    

    Curl Add

  2. Add the following prompt and press Enter to generate the curl command to get the value of a key from the Redis server.:

     Generate a curl command to get the value of a key from the Redis server.
    

Step 6: Test the integration

  1. Click on the Run button to start the integration.
  2. Execute the generated curl commands to add a key-value pair.
       curl -X POST http://localhost:8080/cache/add \
         -H "Content-Type: application/json" \
         -d '{"key": "BI", "value": "Ballerina Integrator is an AI-assisted integration platform."}' 
    
  3. Execute the generated curl command to get the value of the key.
       curl http://localhost:8080/cache/get?key=BI
    
  4. The response will be the value of the key BI stored in the Redis server.
    Ballerina Integrator is an AI-assisted integration platform.%
    

Step 7: Stop the integration

  1. Click on the Stop button to stop the integration.
  2. Stop the Redis server by running the following command:
    docker stop some-redis