# How to write automated tests for APIs with Postman - Part 2

In the [last tutorial](https://blog.postman.com/writing-automated-tests-for-apis-using-postman/ "How to write automated tests for APIs using Postman – Part 1") we discussed how to set up Jetpacks and write a basic test for a sample API endpoint. The test checked the response status code and the Content-Type header against known values. In this tutorial, we'll write a more comprehensive test. We'll use the super-useful [JSONBlob API](https://jsonblob.com/api). This API lets you create, update, and delete content on the server. We'll use it to mimic a blog. This mirrors common API usage scenarios and will be helpful in demonstrating how Postman can make things easier for you. Do remember that the API is only for demo purposes and does not follow the best security practices.

## 1. Create an environment

 In this tutorial, we'll use environment variables. Create an environment with the following values: **url**: https://jsonblob.com [![Screen Shot 2016-06-27 at 16.46.44](https://blog.postman.com/wp-content/uploads/2016/06/Screen-Shot-2016-06-27-at-16.46.44-1024x780.png)](https://blog.postman.com/wp-content/uploads/2016/06/Screen-Shot-2016-06-27-at-16.46.44.png) Select the environment in the environment list to activate it. We'll use test scripts to fill this environment with more values. ## 2. POST request to create a new blog post

 To create a new blog post, we'll need to send a POST request to **{{url}}/api/jsonBlob** with the following request body: ```
{
    "content": "My first blog post :)"
}
```

 [![Screen Shot 2016-06-27 at 18.50.07](https://blog.postman.com/wp-content/uploads/2016/06/Screen-Shot-2016-06-27-at-18.50.07-1024x559.png)](https://blog.postman.com/wp-content/uploads/2016/06/Screen-Shot-2016-06-27-at-18.50.07.png) This endpoint will return a "Location" header, with the URL of the newly created blog post: [![Screen Shot 2016-06-27 at 16.49.24](https://blog.postman.com/wp-content/uploads/2016/06/Screen-Shot-2016-06-27-at-16.49.24-1024x347.png)](https://blog.postman.com/wp-content/uploads/2016/06/Screen-Shot-2016-06-27-at-16.49.24.png) Let's use the test script feature to extract this header and store it: [![Screen Shot 2016-06-27 at 16.49.31](https://blog.postman.com/wp-content/uploads/2014/04/Screen-Shot-2016-06-27-at-16.49.31-1024x366.png)](https://blog.postman.com/wp-content/uploads/2014/04/Screen-Shot-2016-06-27-at-16.49.31.png) Save this request to a collection. ## 3. Validate that the post was created

 We can now make a GET call to the saved URL to make sure the blog post was created. Set the URL to {{blogLink}}, and add a test script to validate the contents. [![Screen Shot 2016-06-27 at 17.39.57](https://blog.postman.com/wp-content/uploads/2016/06/Screen-Shot-2016-06-27-at-17.39.57-1024x466.png)](https://blog.postman.com/wp-content/uploads/2016/06/Screen-Shot-2016-06-27-at-17.39.57.png)## 4. Update the blog post

 We can update existing blog posts using the PUT method. Set the URL to {{blogLink}}, since we know the blog post we want to update. Let's update the blog content to: ```
{
  "content": "My first blog post, updated :)"
}
```

 [![Screen Shot 2016-06-27 at 18.20.31](https://blog.postman.com/wp-content/uploads/2016/06/Screen-Shot-2016-06-27-at-18.20.31-1024x405.png)](https://blog.postman.com/wp-content/uploads/2016/06/Screen-Shot-2016-06-27-at-18.20.31.png)## 5. Verify that the contents have been updated

 We can use a modified version of the GET request we created in step 3. All we need to do is change the test script to: ```
tests["Has correct updated text"] = 
  responseBody.has("My first blog post, updated :)");

```

 [![Screen Shot 2016-06-27 at 17.39.53](https://blog.postman.com/wp-content/uploads/2016/06/Screen-Shot-2016-06-27-at-17.39.53-1024x462.png)](https://blog.postman.com/wp-content/uploads/2016/06/Screen-Shot-2016-06-27-at-17.39.53.png)## 6. Delete the blog post

 Change the GET to DELETE to remove the blog post from the server. [![Screen Shot 2016-06-27 at 16.57.27](https://blog.postman.com/wp-content/uploads/2016/06/Screen-Shot-2016-06-27-at-16.57.27-1024x467.png)](https://blog.postman.com/wp-content/uploads/2016/06/Screen-Shot-2016-06-27-at-16.57.27.png)## 7. Verify that the blog post has been deleted

 Now that we've deleted the post, let's verify that the blog URL no longer returns any content. Use the same URL as the previous GET requests, and change the test script to ```
tests["Status code is 404"] = 
    responseCode.code === 404;

```

 A 404 response code indicates that the entity we requested was not found. [![Screen Shot 2016-06-27 at 16.57.30](https://blog.postman.com/wp-content/uploads/2016/06/Screen-Shot-2016-06-27-at-16.57.30-1024x466.png)](https://blog.postman.com/wp-content/uploads/2016/06/Screen-Shot-2016-06-27-at-16.57.30.png) Running the request says that everything went well. Postman would notify you of errors if something did not go as per expectations. To check for Javascript errors in your code, you can [open the Chrome Console](https://blog.postman.com/enabling-chrome-developer-tools-inside-postman/ "Enabling Chrome Developer Tools inside Postman") and monitor the logs. You can download this collection [here](https://www.postman.com/collections/4078901cf1af5dc19ec2). In the next tutorial, we'll look at the collection runner and how we can run this entire collection in one click using the [Collection Runner](https://learning.postman.com/docs/postman/collection-runs/starting-a-collection-run/). The "How to write automated test with Postman" series1. [Part 1](https://blog.postman.com/writing-automated-tests-for-apis-using-postman/ "How to write automated tests for APIs using Postman – Part 1")
2. Part 2 (this article)
3. [Part 3](https://blog.postman.com/writing-automated-tests-with-postman-part-3/)
 
 ***Update:*** See how to [write tests using the newer PM API](https://blog.postman.com/2017/10/25/writing-tests-in-postman/) (known as the `pm.*`API).