Hoverfly Cloud
Visit Our Main SitePricingTry Hoverfly Cloud
  • Introduction
  • Use cases for API Simulation
  • Key concepts
  • What's New
  • Dashboard
  • Simulation
    • Create simulations
    • Configuring Request Matchers
      • JSON Request Matchers on the Body
        • EXACT Matcher
        • JSON Matcher
        • JSON Partial Matcher
        • JSONPath Matcher
        • Regular Expression Matcher
      • Handling the response when Hoverfly cannot match
    • Building a JSON Response
      • Using a JSON object from the Request
      • Looping over a JSON array from the Request
    • Simulating Webhooks and Callbacks
    • Using templating in simulations
      • Render back any part of the Request
      • Render synthetic data
      • Faker functions
      • Render dates and times with offsets
      • Conditional logic
      • Looping over arrays
      • Variables and Arrays
      • Arithmetic operations
      • String operations
      • Validation operations
      • Simulating a persistent backend
      • Transitioning state between requests
      • Combining and nesting templating functions
      • Useful helper functions
      • Avoiding HTML encoding in function return values
    • Using data sources in simulations
      • Querying data sources
      • Updating data in data sources
      • Deleting data from data sources
      • Inserting data into a data source
      • Guidance on using the SQL like syntax
    • Working with Simulation Files
  • Service
    • Start a new service
    • Use a service
    • Update a service
    • Configuring Journal Indexing
  • Command line interface
    • Hoverfly Cloud CLI commands
  • Tutorials
    • Quickstart
    • Creating simulations and services
      • Tutorial 1: Create a service and simulation manually
      • Tutorial 2: Create a service and simulation by importing from a Swagger specification
      • Tutorial 3: Create a service and simulation by capturing real API traffic
    • Response Templating
      • Tutorial 4: Response Templating
    • Hoverfly service modes
      • Tutorial 5: Simulate, Capture, Spy and Passthrough modes
    • Automating with the CLI and API
      • Tutorial 6: Using the CLI and the Hoverfly Cloud API
Powered by GitBook
On this page
  • Attaching a data source to a simulation
  • Looking up a data value from within the response template
  1. Simulation

Using data sources in simulations

Hoverfly Cloud supports CSV data sources for Enterprise customers to do key value data lookups and simulate persistence within simulations.

PreviousAvoiding HTML encoding in function return valuesNextQuerying data sources

Last updated 3 months ago

Attaching a data source to a simulation

You can upload a csv file when editing your simulation from the Simulation tab.

You can both query data from a CSV data source as well as manipulate data within a data source by adding to it and deleting from it. Hoverfly supports a number of templating methods for simple read, update and delete functions. In addition Hoverfly supports a templating function that allows simple SQL like commands for SELECT, UPDATE and DELETE.

Note that you cannot edit the data sources on a simulation already running on a service. You need to add and remove data sources from the master copy of the simulation, and then start a service running a copy of that simulation.

Looking up a data value from within the response template

To use the data in the csv file, you need to use the {{ csv }} templating function.

{{csv '<data-source-name>' '<key-column>' '<key-value>' '<return-column>'}}

So for example given a data source named "products" with the following csv structure:

You can use the following syntax to render the word "shirt" in a response template (note the use of single quotes):

{{csv 'products' 'productId' '101' 'productName'}}

Or if for example the productId was provided at position 1 on the path:

{{csv 'products' 'productId' (Request.Path.[1]) 'productName'}}

Or if you needed to loop over some JSON array of productIds from the request body and render back data doing lookups for each productId:

{{#each (Request.Body 'jsonpath' '$.products')}}  
ProductId {{this}} is {{csv 'products' 'productId' this 'productName'}}
{{/each}}

There are a number of functions to allow querying, updating, deleting and inserting data into the csv data sources. You can read more about them below:

Querying csv data sources
Updating csv data sources
Deleting data from csv data sources
Inserting data into csv data sources
Guidance on using the SQL like syntax
Add CSV data sources